1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace League\OAuth2\Client\Provider; |
4
|
|
|
|
5
|
|
|
use League\OAuth2\Client\Provider\Exception\InstagramIdentityProviderException; |
6
|
|
|
use League\OAuth2\Client\Token\AccessToken; |
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
8
|
|
|
|
9
|
|
|
class Instagram extends AbstractProvider |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var string Key used in a token response to identify the resource owner. |
13
|
|
|
*/ |
14
|
|
|
const ACCESS_TOKEN_RESOURCE_OWNER_ID = 'user_id'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Default scopes |
18
|
|
|
* |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
public $defaultScopes = ['user_profile']; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Default host |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $host = 'https://api.instagram.com'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Default Graph API host |
32
|
|
|
* |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $graphHost = 'https://graph.instagram.com'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Gets host. |
39
|
|
|
* |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
4 |
|
public function getHost() |
43
|
|
|
{ |
44
|
4 |
|
return $this->host; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Gets Graph API host. |
49
|
|
|
* |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
4 |
|
public function getGraphHost() |
53
|
|
|
{ |
54
|
4 |
|
return $this->graphHost; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get the string used to separate scopes. |
59
|
|
|
* |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
6 |
|
protected function getScopeSeparator() |
63
|
|
|
{ |
64
|
6 |
|
return ' '; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get authorization url to begin OAuth flow |
69
|
|
|
* |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
6 |
|
public function getBaseAuthorizationUrl() |
73
|
|
|
{ |
74
|
6 |
|
return $this->host.'/oauth/authorize'; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get access token url to retrieve token |
79
|
|
|
* |
80
|
|
|
* @param array $params |
81
|
|
|
* |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
12 |
|
public function getBaseAccessTokenUrl(array $params) |
85
|
|
|
{ |
86
|
12 |
|
return $this->host.'/oauth/access_token'; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Get provider url to fetch user details |
91
|
|
|
* |
92
|
|
|
* @param AccessToken $token |
93
|
|
|
* |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
2 |
|
public function getResourceOwnerDetailsUrl(AccessToken $token) |
97
|
|
|
{ |
98
|
2 |
|
return $this->graphHost.'/me?fields=id,username&access_token='.$token; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Returns an authenticated PSR-7 request instance. |
103
|
|
|
* |
104
|
|
|
* @param string $method |
105
|
|
|
* @param string $url |
106
|
|
|
* @param AccessToken|string $token |
107
|
|
|
* @param array $options Any of "headers", "body", and "protocolVersion". |
108
|
|
|
* |
109
|
|
|
* @return \Psr\Http\Message\RequestInterface |
110
|
|
|
*/ |
111
|
4 |
|
public function getAuthenticatedRequest($method, $url, $token, array $options = []) |
112
|
|
|
{ |
113
|
4 |
|
$parsedUrl = parse_url($url); |
114
|
4 |
|
$queryString = array(); |
115
|
|
|
|
116
|
4 |
|
if (isset($parsedUrl['query'])) { |
117
|
2 |
|
parse_str($parsedUrl['query'], $queryString); |
118
|
|
|
} |
119
|
|
|
|
120
|
4 |
|
if (!isset($queryString['access_token'])) { |
121
|
2 |
|
$queryString['access_token'] = (string) $token; |
122
|
|
|
} |
123
|
|
|
|
124
|
4 |
|
$url = http_build_url($url, [ |
125
|
4 |
|
'query' => http_build_query($queryString), |
126
|
|
|
]); |
127
|
|
|
|
128
|
4 |
|
return $this->createRequest($method, $url, null, $options); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Get the default scopes used by this provider. |
133
|
|
|
* |
134
|
|
|
* This should not be a complete list of all scopes, but the minimum |
135
|
|
|
* required for the provider user interface! |
136
|
|
|
* |
137
|
|
|
* @return array |
138
|
|
|
*/ |
139
|
4 |
|
protected function getDefaultScopes() |
140
|
|
|
{ |
141
|
4 |
|
return $this->defaultScopes; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Check a provider response for errors. |
146
|
|
|
* |
147
|
|
|
* @throws IdentityProviderException |
148
|
|
|
* @param ResponseInterface $response |
149
|
|
|
* @param string $data Parsed response data |
150
|
|
|
* @return void |
151
|
|
|
*/ |
152
|
10 |
|
protected function checkResponse(ResponseInterface $response, $data) |
153
|
|
|
{ |
154
|
|
|
// Standard error response format |
155
|
10 |
|
if (!empty($data['error'])) { |
156
|
2 |
|
throw InstagramIdentityProviderException::clientException($response, $data); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
// OAuthException error response format |
160
|
8 |
|
if (!empty($data['error_type'])) { |
161
|
2 |
|
throw InstagramIdentityProviderException::oauthException($response, $data); |
162
|
|
|
} |
163
|
6 |
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Generate a user object from a successful user details request. |
167
|
|
|
* |
168
|
|
|
* @param array $response |
169
|
|
|
* @param AccessToken $token |
170
|
|
|
* @return ResourceOwnerInterface |
171
|
|
|
*/ |
172
|
2 |
|
protected function createResourceOwner(array $response, AccessToken $token) |
173
|
|
|
{ |
174
|
2 |
|
return new InstagramResourceOwner($response); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Sets host. |
179
|
|
|
* |
180
|
|
|
* @param string $host |
181
|
|
|
* |
182
|
|
|
* @return string |
183
|
|
|
*/ |
184
|
2 |
|
public function setHost($host) |
185
|
|
|
{ |
186
|
2 |
|
$this->host = $host; |
187
|
|
|
|
188
|
2 |
|
return $this; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Sets Graph API host. |
193
|
|
|
* |
194
|
|
|
* @param string $host |
195
|
|
|
* |
196
|
|
|
* @return string |
197
|
|
|
*/ |
198
|
2 |
|
public function setGraphHost($host) |
199
|
|
|
{ |
200
|
2 |
|
$this->graphHost = $host; |
201
|
|
|
|
202
|
2 |
|
return $this; |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|