├── Provider.php ├── README.md ├── YouTubeExtendSocialite.php └── composer.json /Provider.php: -------------------------------------------------------------------------------- 1 | buildAuthUrlFromBase('https://accounts.google.com/o/oauth2/v2/auth', $state); 22 | } 23 | 24 | protected function getTokenUrl(): string 25 | { 26 | return 'https://oauth2.googleapis.com/token'; 27 | } 28 | 29 | /** 30 | * {@inheritdoc} 31 | */ 32 | protected function getUserByToken($token) 33 | { 34 | $response = $this->getHttpClient()->get('https://www.googleapis.com/youtube/v3/channels', [ 35 | RequestOptions::HEADERS => [ 36 | 'Authorization' => 'Bearer '.$token, 37 | ], 38 | RequestOptions::QUERY => [ 39 | 'part' => 'snippet', 40 | 'mine' => 'true', 41 | ], 42 | ]); 43 | 44 | $responseJson = json_decode((string) $response->getBody(), true); 45 | 46 | return $responseJson['items'][0] ?? []; 47 | } 48 | 49 | /** 50 | * {@inheritdoc} 51 | */ 52 | protected function mapUserToObject(array $user) 53 | { 54 | return (new User)->setRaw($user)->map([ 55 | 'id' => $user['id'] ?? null, 56 | 'nickname' => $user['snippet']['title'] ?? null, 57 | 'name' => null, 58 | 'email' => null, 59 | 'avatar' => $user['snippet']['thumbnails']['high']['url'] ?? null, 60 | ]); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # YouTube 2 | 3 | ```bash 4 | composer require socialiteproviders/youtube 5 | ``` 6 | 7 | ### Important Note 8 | 9 | If the user does not have a youtube channel, all the user object fields will be `null`. Youtube no longer automatically creates channels. Consider using the main Google provider instead. 10 | 11 | ## Installation & Basic Usage 12 | 13 | Please see the [Base Installation Guide](https://socialiteproviders.com/usage/), then follow the provider specific instructions below. 14 | 15 | ### Add configuration to `config/services.php` 16 | 17 | ```php 18 | 'youtube' => [ 19 | 'client_id' => env('YOUTUBE_CLIENT_ID'), 20 | 'client_secret' => env('YOUTUBE_CLIENT_SECRET'), 21 | 'redirect' => env('YOUTUBE_REDIRECT_URI') 22 | ], 23 | ``` 24 | 25 | ### Add provider event listener 26 | 27 | #### Laravel 11+ 28 | 29 | In Laravel 11, the default `EventServiceProvider` provider was removed. Instead, add the listener using the `listen` method on the `Event` facade, in your `AppServiceProvider` `boot` method. 30 | 31 | * Note: You do not need to add anything for the built-in socialite providers unless you override them with your own providers. 32 | 33 | ```php 34 | Event::listen(function (\SocialiteProviders\Manager\SocialiteWasCalled $event) { 35 | $event->extendSocialite('youtube', \SocialiteProviders\YouTube\Provider::class); 36 | }); 37 | ``` 38 |
39 | 40 | Laravel 10 or below 41 | 42 | Configure the package's listener to listen for `SocialiteWasCalled` events. 43 | 44 | Add the event to your `listen[]` array in `app/Providers/EventServiceProvider`. See the [Base Installation Guide](https://socialiteproviders.com/usage/) for detailed instructions. 45 | 46 | ```php 47 | protected $listen = [ 48 | \SocialiteProviders\Manager\SocialiteWasCalled::class => [ 49 | // ... other providers 50 | \SocialiteProviders\YouTube\YouTubeExtendSocialite::class.'@handle', 51 | ], 52 | ]; 53 | ``` 54 |
55 | 56 | ### Usage 57 | 58 | You should now be able to use the provider like you would regularly use Socialite (assuming you have the facade installed): 59 | 60 | ```php 61 | return Socialite::driver('youtube')->redirect(); 62 | ``` 63 | 64 | ### Returned User fields 65 | 66 | - ``id`` 67 | - ``nickname`` 68 | - ``avatar`` 69 | -------------------------------------------------------------------------------- /YouTubeExtendSocialite.php: -------------------------------------------------------------------------------- 1 | extendSocialite('youtube', Provider::class); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "socialiteproviders/youtube", 3 | "description": "YouTube OAuth2 Provider for Laravel Socialite", 4 | "license": "MIT", 5 | "keywords": [ 6 | "laravel", 7 | "oauth", 8 | "provider", 9 | "socialite", 10 | "youtube" 11 | ], 12 | "authors": [ 13 | { 14 | "name": "Brian Faust", 15 | "email": "hello@brianfaust.de" 16 | } 17 | ], 18 | "support": { 19 | "issues": "https://github.com/socialiteproviders/providers/issues", 20 | "source": "https://github.com/socialiteproviders/providers", 21 | "docs": "https://socialiteproviders.com/youtube" 22 | }, 23 | "require": { 24 | "php": "^8.0", 25 | "ext-json": "*", 26 | "socialiteproviders/manager": "^4.4" 27 | }, 28 | "autoload": { 29 | "psr-4": { 30 | "SocialiteProviders\\YouTube\\": "" 31 | } 32 | } 33 | } 34 | --------------------------------------------------------------------------------