|
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 InstagramProvider extends AbstractProvider implements ProviderInterface |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* {@inheritdoc} |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $scopeSeparator = ' '; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* {@inheritdoc} |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $scopes = ['basic']; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* {@inheritdoc} |
|
23
|
|
|
*/ |
|
24
|
|
|
protected function getAuthUrl($state) |
|
25
|
|
|
{ |
|
26
|
|
|
return $this->buildAuthUrlFromBase( |
|
27
|
|
|
'https://api.instagram.com/oauth/authorize', $state |
|
28
|
|
|
); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* {@inheritdoc} |
|
33
|
|
|
*/ |
|
34
|
|
|
protected function getTokenUrl() |
|
35
|
|
|
{ |
|
36
|
|
|
return 'https://api.instagram.com/oauth/access_token'; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* {@inheritdoc} |
|
41
|
|
|
*/ |
|
42
|
|
|
protected function getUserByToken($token) |
|
43
|
|
|
{ |
|
44
|
|
|
$endpoint = '/users/self'; |
|
45
|
|
|
$query = [ |
|
46
|
|
|
'access_token' => $token, |
|
47
|
|
|
]; |
|
48
|
|
|
$signature = $this->generateSignature($endpoint, $query); |
|
49
|
|
|
|
|
50
|
|
|
$query['sig'] = $signature; |
|
51
|
|
|
$response = $this->getHttpClient()->get( |
|
52
|
|
|
'https://api.instagram.com/v1/users/self', [ |
|
53
|
|
|
'query' => $query, |
|
54
|
|
|
'headers' => [ |
|
55
|
|
|
'Accept' => 'application/json', |
|
56
|
|
|
], |
|
57
|
|
|
]); |
|
58
|
|
|
|
|
59
|
|
|
return json_decode($response->getBody()->getContents(), true)['data']; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* {@inheritdoc} |
|
64
|
|
|
*/ |
|
65
|
|
|
protected function mapUserToObject(array $user) |
|
66
|
|
|
{ |
|
67
|
|
|
return (new User())->setRaw($user)->map([ |
|
68
|
|
|
'id' => $user['id'], |
|
69
|
|
|
'nickname' => $user['username'], |
|
70
|
|
|
'name' => $user['full_name'], |
|
71
|
|
|
'email' => null, |
|
72
|
|
|
'avatar' => $user['profile_picture'], |
|
73
|
|
|
]); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* {@inheritdoc} |
|
78
|
|
|
*/ |
|
79
|
|
|
public function getAccessToken($code) |
|
80
|
|
|
{ |
|
81
|
|
|
$response = $this->getHttpClient()->post($this->getTokenUrl(), [ |
|
82
|
|
|
'form_params' => $this->getTokenFields($code), |
|
83
|
|
|
]); |
|
84
|
|
|
|
|
85
|
|
|
$this->credentialsResponseBody = json_decode($response->getBody(), true); |
|
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
return $this->parseAccessToken($response->getBody()); |
|
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* {@inheritdoc} |
|
92
|
|
|
*/ |
|
93
|
|
|
protected function getTokenFields($code) |
|
94
|
|
|
{ |
|
95
|
|
|
return array_merge(parent::getTokenFields($code), [ |
|
96
|
|
|
'grant_type' => 'authorization_code', |
|
97
|
|
|
]); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Allows compatibility for signed API requests. |
|
102
|
|
|
*/ |
|
103
|
|
|
protected function generateSignature($endpoint, array $params) |
|
104
|
|
|
{ |
|
105
|
|
|
$sig = $endpoint; |
|
106
|
|
|
ksort($params); |
|
107
|
|
|
foreach ($params as $key => $val) { |
|
108
|
|
|
$sig .= "|$key=$val"; |
|
109
|
|
|
} |
|
110
|
|
|
$signing_key = $this->clientSecret; |
|
111
|
|
|
|
|
112
|
|
|
return hash_hmac('sha256', $sig, $signing_key, false); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|