1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mckenziearts\LaravelOAuth\Providers; |
4
|
|
|
|
5
|
|
|
use Laravel\Socialite\Two\User; |
6
|
|
|
use Laravel\Socialite\Two\AbstractProvider; |
7
|
|
|
use Laravel\Socialite\Two\ProviderInterface; |
8
|
|
|
|
9
|
|
|
class YoutubeProvider extends AbstractProvider implements ProviderInterface |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* {@inheritdoc} |
13
|
|
|
*/ |
14
|
|
|
protected $scopes = ['https://www.googleapis.com/auth/youtube.readonly']; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* {@inheritdoc} |
18
|
|
|
*/ |
19
|
|
|
protected $scopeSeparator = ' '; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* {@inheritdoc} |
23
|
|
|
*/ |
24
|
|
|
protected function getAuthUrl($state) |
25
|
|
|
{ |
26
|
|
|
return $this->buildAuthUrlFromBase( |
27
|
|
|
'https://accounts.google.com/o/oauth2/auth', $state |
28
|
|
|
); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* {@inheritdoc} |
33
|
|
|
*/ |
34
|
|
|
protected function getTokenUrl() |
35
|
|
|
{ |
36
|
|
|
return 'https://accounts.google.com/o/oauth2/token'; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
|
|
protected function getUserByToken($token) |
43
|
|
|
{ |
44
|
|
|
$response = $this->getHttpClient()->get( |
45
|
|
|
'https://www.googleapis.com/youtube/v3/channels?part=snippet&mine=true', [ |
46
|
|
|
'headers' => [ |
47
|
|
|
'Authorization' => 'Bearer '.$token, |
48
|
|
|
], |
49
|
|
|
]); |
50
|
|
|
|
51
|
|
|
return json_decode($response->getBody()->getContents(), true)['items'][0]; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
|
|
protected function mapUserToObject(array $user) |
58
|
|
|
{ |
59
|
|
|
return (new User())->setRaw($user)->map([ |
60
|
|
|
'id' => $user['id'], |
61
|
|
|
'nickname' => $user['snippet']['title'], |
62
|
|
|
'name' => null, |
63
|
|
|
'email' => null, |
64
|
|
|
'avatar' => $user['snippet']['thumbnails']['high']['url'], |
65
|
|
|
]); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritdoc} |
70
|
|
|
*/ |
71
|
|
|
protected function getTokenFields($code) |
72
|
|
|
{ |
73
|
|
|
return array_merge(parent::getTokenFields($code), [ |
74
|
|
|
'grant_type' => 'authorization_code', |
75
|
|
|
]); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|