1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OAuth\OAuth2\Service; |
4
|
|
|
|
5
|
|
|
use OAuth\Common\Consumer\CredentialsInterface; |
6
|
|
|
use OAuth\Common\Http\Client\ClientInterface; |
7
|
|
|
use OAuth\Common\Http\Exception\TokenResponseException; |
8
|
|
|
use OAuth\Common\Http\Uri\Uri; |
9
|
|
|
use OAuth\Common\Http\Uri\UriInterface; |
10
|
|
|
use OAuth\Common\Storage\TokenStorageInterface; |
11
|
|
|
use OAuth\OAuth2\Token\StdOAuth2Token; |
12
|
|
|
|
13
|
|
|
class Instagram extends AbstractService |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Defined scopes. |
17
|
|
|
* |
18
|
|
|
* @see http://instagram.com/developer/authentication/#scope |
19
|
|
|
*/ |
20
|
|
|
const SCOPE_BASIC = 'basic'; |
21
|
|
|
const SCOPE_PUBLIC_CONTENT = 'public_content'; |
22
|
|
|
const SCOPE_COMMENTS = 'comments'; |
23
|
|
|
const SCOPE_RELATIONSHIPS = 'relationships'; |
24
|
|
|
const SCOPE_LIKES = 'likes'; |
25
|
|
|
const SCOPE_FOLLOWER_LIST = 'follower_list'; |
26
|
|
|
|
27
|
|
|
public function __construct( |
28
|
|
|
CredentialsInterface $credentials, |
29
|
|
|
ClientInterface $httpClient, |
30
|
|
|
TokenStorageInterface $storage, |
31
|
|
|
$scopes = [], |
32
|
|
|
?UriInterface $baseApiUri = null |
33
|
|
|
) { |
34
|
|
|
parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri, true); |
35
|
|
|
|
36
|
|
|
if (null === $baseApiUri) { |
37
|
|
|
$this->baseApiUri = new Uri('https://api.instagram.com/v1/'); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
|
|
public function getAuthorizationEndpoint() |
45
|
|
|
{ |
46
|
|
|
return new Uri('https://api.instagram.com/oauth/authorize/'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
|
|
public function getAccessTokenEndpoint() |
53
|
|
|
{ |
54
|
|
|
return new Uri('https://api.instagram.com/oauth/access_token'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
|
|
protected function getAuthorizationMethod() |
61
|
|
|
{ |
62
|
|
|
return static::AUTHORIZATION_METHOD_QUERY_STRING; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
|
|
protected function parseAccessTokenResponse($responseBody) |
69
|
|
|
{ |
70
|
|
|
$data = json_decode($responseBody, true); |
71
|
|
|
|
72
|
|
|
if (null === $data || !is_array($data)) { |
73
|
|
|
throw new TokenResponseException('Unable to parse response.'); |
74
|
|
|
} elseif (isset($data['error'])) { |
75
|
|
|
throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$token = new StdOAuth2Token(); |
79
|
|
|
$token->setAccessToken($data['access_token']); |
80
|
|
|
// Instagram tokens evidently never expire... |
81
|
|
|
$token->setEndOfLife(StdOAuth2Token::EOL_NEVER_EXPIRES); |
82
|
|
|
unset($data['access_token']); |
83
|
|
|
|
84
|
|
|
$token->setExtraParams($data); |
85
|
|
|
|
86
|
|
|
return $token; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|