@@ -17,240 +17,240 @@ |
||
17 | 17 | */ |
18 | 18 | class Instagram extends OAuth2 |
19 | 19 | { |
20 | - /** |
|
21 | - * {@inheritdoc} |
|
22 | - */ |
|
23 | - protected $scope = 'user_profile,user_media'; |
|
24 | - |
|
25 | - /** |
|
26 | - * {@inheritdoc} |
|
27 | - */ |
|
28 | - protected $apiBaseUrl = 'https://graph.instagram.com'; |
|
29 | - |
|
30 | - /** |
|
31 | - * {@inheritdoc} |
|
32 | - */ |
|
33 | - protected $authorizeUrl = 'https://api.instagram.com/oauth/authorize'; |
|
34 | - |
|
35 | - /** |
|
36 | - * {@inheritdoc} |
|
37 | - */ |
|
38 | - protected $accessTokenUrl = 'https://api.instagram.com/oauth/access_token'; |
|
39 | - |
|
40 | - /** |
|
41 | - * {@inheritdoc} |
|
42 | - */ |
|
43 | - protected $apiDocumentation = 'https://developers.facebook.com/docs/instagram-basic-display-api'; |
|
44 | - |
|
45 | - /** |
|
46 | - * {@inheritdoc} |
|
47 | - */ |
|
48 | - protected function initialize() |
|
49 | - { |
|
50 | - parent::initialize(); |
|
51 | - |
|
52 | - // The Instagram API requires an access_token from authenticated users |
|
53 | - // for each endpoint. |
|
54 | - $accessToken = $this->getStoredData($this->accessTokenName); |
|
55 | - $this->apiRequestParameters[$this->accessTokenName] = $accessToken; |
|
56 | - } |
|
57 | - |
|
58 | - /** |
|
59 | - * {@inheritdoc} |
|
60 | - */ |
|
61 | - protected function validateAccessTokenExchange($response) |
|
62 | - { |
|
63 | - $collection = parent::validateAccessTokenExchange($response); |
|
64 | - |
|
65 | - if (!$collection->exists('expires_in')) { |
|
66 | - // Instagram tokens always expire in an hour, but this is implicit not explicit |
|
67 | - |
|
68 | - $expires_in = 60 * 60; |
|
69 | - |
|
70 | - $expires_at = time() + $expires_in; |
|
71 | - |
|
72 | - $this->storeData('expires_in', $expires_in); |
|
73 | - $this->storeData('expires_at', $expires_at); |
|
74 | - } |
|
75 | - |
|
76 | - return $collection; |
|
77 | - } |
|
78 | - |
|
79 | - /** |
|
80 | - * {@inheritdoc} |
|
81 | - */ |
|
82 | - public function maintainToken() |
|
83 | - { |
|
84 | - if (!$this->isConnected()) { |
|
85 | - return; |
|
86 | - } |
|
87 | - |
|
88 | - // Handle token exchange prior to the standard handler for an API request |
|
89 | - $exchange_by_expiry_days = $this->config->get('exchange_by_expiry_days') ?: 45; |
|
90 | - if ($exchange_by_expiry_days !== null) { |
|
91 | - $projected_timestamp = time() + 60 * 60 * 24 * $exchange_by_expiry_days; |
|
92 | - if (!$this->hasAccessTokenExpired() && $this->hasAccessTokenExpired($projected_timestamp)) { |
|
93 | - $this->exchangeAccessToken(); |
|
94 | - } |
|
95 | - } |
|
96 | - } |
|
97 | - |
|
98 | - /** |
|
99 | - * Exchange the Access Token with one that expires further in the future. |
|
100 | - * |
|
101 | - * @return string Raw Provider API response |
|
102 | - * @throws \Hybridauth\Exception\HttpClientFailureException |
|
103 | - * @throws \Hybridauth\Exception\HttpRequestFailedException |
|
104 | - * @throws InvalidAccessTokenException |
|
105 | - */ |
|
106 | - public function exchangeAccessToken() |
|
107 | - { |
|
108 | - if ($this->getStoredData('expires_in') >= 5000000) { |
|
109 | - /* |
|
20 | + /** |
|
21 | + * {@inheritdoc} |
|
22 | + */ |
|
23 | + protected $scope = 'user_profile,user_media'; |
|
24 | + |
|
25 | + /** |
|
26 | + * {@inheritdoc} |
|
27 | + */ |
|
28 | + protected $apiBaseUrl = 'https://graph.instagram.com'; |
|
29 | + |
|
30 | + /** |
|
31 | + * {@inheritdoc} |
|
32 | + */ |
|
33 | + protected $authorizeUrl = 'https://api.instagram.com/oauth/authorize'; |
|
34 | + |
|
35 | + /** |
|
36 | + * {@inheritdoc} |
|
37 | + */ |
|
38 | + protected $accessTokenUrl = 'https://api.instagram.com/oauth/access_token'; |
|
39 | + |
|
40 | + /** |
|
41 | + * {@inheritdoc} |
|
42 | + */ |
|
43 | + protected $apiDocumentation = 'https://developers.facebook.com/docs/instagram-basic-display-api'; |
|
44 | + |
|
45 | + /** |
|
46 | + * {@inheritdoc} |
|
47 | + */ |
|
48 | + protected function initialize() |
|
49 | + { |
|
50 | + parent::initialize(); |
|
51 | + |
|
52 | + // The Instagram API requires an access_token from authenticated users |
|
53 | + // for each endpoint. |
|
54 | + $accessToken = $this->getStoredData($this->accessTokenName); |
|
55 | + $this->apiRequestParameters[$this->accessTokenName] = $accessToken; |
|
56 | + } |
|
57 | + |
|
58 | + /** |
|
59 | + * {@inheritdoc} |
|
60 | + */ |
|
61 | + protected function validateAccessTokenExchange($response) |
|
62 | + { |
|
63 | + $collection = parent::validateAccessTokenExchange($response); |
|
64 | + |
|
65 | + if (!$collection->exists('expires_in')) { |
|
66 | + // Instagram tokens always expire in an hour, but this is implicit not explicit |
|
67 | + |
|
68 | + $expires_in = 60 * 60; |
|
69 | + |
|
70 | + $expires_at = time() + $expires_in; |
|
71 | + |
|
72 | + $this->storeData('expires_in', $expires_in); |
|
73 | + $this->storeData('expires_at', $expires_at); |
|
74 | + } |
|
75 | + |
|
76 | + return $collection; |
|
77 | + } |
|
78 | + |
|
79 | + /** |
|
80 | + * {@inheritdoc} |
|
81 | + */ |
|
82 | + public function maintainToken() |
|
83 | + { |
|
84 | + if (!$this->isConnected()) { |
|
85 | + return; |
|
86 | + } |
|
87 | + |
|
88 | + // Handle token exchange prior to the standard handler for an API request |
|
89 | + $exchange_by_expiry_days = $this->config->get('exchange_by_expiry_days') ?: 45; |
|
90 | + if ($exchange_by_expiry_days !== null) { |
|
91 | + $projected_timestamp = time() + 60 * 60 * 24 * $exchange_by_expiry_days; |
|
92 | + if (!$this->hasAccessTokenExpired() && $this->hasAccessTokenExpired($projected_timestamp)) { |
|
93 | + $this->exchangeAccessToken(); |
|
94 | + } |
|
95 | + } |
|
96 | + } |
|
97 | + |
|
98 | + /** |
|
99 | + * Exchange the Access Token with one that expires further in the future. |
|
100 | + * |
|
101 | + * @return string Raw Provider API response |
|
102 | + * @throws \Hybridauth\Exception\HttpClientFailureException |
|
103 | + * @throws \Hybridauth\Exception\HttpRequestFailedException |
|
104 | + * @throws InvalidAccessTokenException |
|
105 | + */ |
|
106 | + public function exchangeAccessToken() |
|
107 | + { |
|
108 | + if ($this->getStoredData('expires_in') >= 5000000) { |
|
109 | + /* |
|
110 | 110 | Refresh a long-lived token (needed on Instagram, but not Facebook). |
111 | 111 | It's not an oAuth style refresh using a refresh token. |
112 | 112 | Actually it's really just another exchange, and invalidates the old token. |
113 | 113 | Facebook/Instagram documentation is not very helpful at explaining that! |
114 | 114 | */ |
115 | - $exchangeTokenParameters = [ |
|
116 | - 'grant_type' => 'ig_refresh_token', |
|
117 | - 'client_secret' => $this->clientSecret, |
|
118 | - 'access_token' => $this->getStoredData('access_token'), |
|
119 | - ]; |
|
120 | - $url = 'https://graph.instagram.com/refresh_access_token'; |
|
121 | - } else { |
|
122 | - // Exchange short-lived to long-lived |
|
123 | - $exchangeTokenParameters = [ |
|
124 | - 'grant_type' => 'ig_exchange_token', |
|
125 | - 'client_secret' => $this->clientSecret, |
|
126 | - 'access_token' => $this->getStoredData('access_token'), |
|
127 | - ]; |
|
128 | - $url = 'https://graph.instagram.com/access_token'; |
|
129 | - } |
|
130 | - |
|
131 | - $response = $this->httpClient->request( |
|
132 | - $url, |
|
133 | - 'GET', |
|
134 | - $exchangeTokenParameters |
|
135 | - ); |
|
136 | - |
|
137 | - $this->validateApiResponse('Unable to exchange the access token'); |
|
138 | - |
|
139 | - $this->validateAccessTokenExchange($response); |
|
140 | - |
|
141 | - return $response; |
|
142 | - } |
|
143 | - |
|
144 | - /** |
|
145 | - * {@inheritdoc} |
|
146 | - */ |
|
147 | - public function getUserProfile() |
|
148 | - { |
|
149 | - $response = $this->apiRequest('me', 'GET', [ |
|
150 | - 'fields' => 'id,username,account_type,media_count', |
|
151 | - ]); |
|
152 | - |
|
153 | - $data = new Collection($response); |
|
154 | - if (!$data->exists('id')) { |
|
155 | - throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); |
|
156 | - } |
|
157 | - |
|
158 | - $userProfile = new User\Profile(); |
|
159 | - $userProfile->identifier = $data->get('id'); |
|
160 | - $userProfile->displayName = $data->get('username'); |
|
161 | - $userProfile->profileURL = "https://instagram.com/{$userProfile->displayName}"; |
|
162 | - $userProfile->data = [ |
|
163 | - 'account_type' => $data->get('account_type'), |
|
164 | - 'media_count' => $data->get('media_count'), |
|
165 | - ]; |
|
166 | - |
|
167 | - return $userProfile; |
|
168 | - } |
|
169 | - |
|
170 | - /** |
|
171 | - * Fetch user medias. |
|
172 | - * |
|
173 | - * @param int $limit Number of elements per page. |
|
174 | - * @param string $pageId Current pager ID. |
|
175 | - * @param array|null $fields Fields to fetch per media. |
|
176 | - * |
|
177 | - * @return \Hybridauth\Data\Collection |
|
178 | - * |
|
179 | - * @throws \Hybridauth\Exception\HttpClientFailureException |
|
180 | - * @throws \Hybridauth\Exception\HttpRequestFailedException |
|
181 | - * @throws \Hybridauth\Exception\InvalidAccessTokenException |
|
182 | - * @throws \Hybridauth\Exception\UnexpectedApiResponseException |
|
183 | - */ |
|
184 | - public function getUserMedia($limit = 12, $pageId = null, array $fields = null) |
|
185 | - { |
|
186 | - if (empty($fields)) { |
|
187 | - $fields = [ |
|
188 | - 'id', |
|
189 | - 'caption', |
|
190 | - 'media_type', |
|
191 | - 'media_url', |
|
192 | - 'thumbnail_url', |
|
193 | - 'permalink', |
|
194 | - 'timestamp', |
|
195 | - 'username', |
|
196 | - ]; |
|
197 | - } |
|
198 | - |
|
199 | - $params = [ |
|
200 | - 'fields' => implode(',', $fields), |
|
201 | - 'limit' => $limit, |
|
202 | - ]; |
|
203 | - if ($pageId !== null) { |
|
204 | - $params['after'] = $pageId; |
|
205 | - } |
|
206 | - |
|
207 | - $response = $this->apiRequest('me/media', 'GET', $params); |
|
208 | - |
|
209 | - $data = new Collection($response); |
|
210 | - if (!$data->exists('data')) { |
|
211 | - throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); |
|
212 | - } |
|
213 | - |
|
214 | - return $data; |
|
215 | - } |
|
216 | - |
|
217 | - /** |
|
218 | - * Fetches a single user's media. |
|
219 | - * |
|
220 | - * @param string $mediaId Media ID. |
|
221 | - * @param array|null $fields Fields to fetch per media. |
|
222 | - * |
|
223 | - * @return \Hybridauth\Data\Collection |
|
224 | - * |
|
225 | - * @throws \Hybridauth\Exception\HttpClientFailureException |
|
226 | - * @throws \Hybridauth\Exception\HttpRequestFailedException |
|
227 | - * @throws \Hybridauth\Exception\InvalidAccessTokenException |
|
228 | - * @throws \Hybridauth\Exception\UnexpectedApiResponseException |
|
229 | - */ |
|
230 | - public function getMedia($mediaId, array $fields = null) |
|
231 | - { |
|
232 | - if (empty($fields)) { |
|
233 | - $fields = [ |
|
234 | - 'id', |
|
235 | - 'caption', |
|
236 | - 'media_type', |
|
237 | - 'media_url', |
|
238 | - 'thumbnail_url', |
|
239 | - 'permalink', |
|
240 | - 'timestamp', |
|
241 | - 'username', |
|
242 | - ]; |
|
243 | - } |
|
244 | - |
|
245 | - $response = $this->apiRequest($mediaId, 'GET', [ |
|
246 | - 'fields' => implode(',', $fields), |
|
247 | - ]); |
|
248 | - |
|
249 | - $data = new Collection($response); |
|
250 | - if (!$data->exists('id')) { |
|
251 | - throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); |
|
252 | - } |
|
253 | - |
|
254 | - return $data; |
|
255 | - } |
|
115 | + $exchangeTokenParameters = [ |
|
116 | + 'grant_type' => 'ig_refresh_token', |
|
117 | + 'client_secret' => $this->clientSecret, |
|
118 | + 'access_token' => $this->getStoredData('access_token'), |
|
119 | + ]; |
|
120 | + $url = 'https://graph.instagram.com/refresh_access_token'; |
|
121 | + } else { |
|
122 | + // Exchange short-lived to long-lived |
|
123 | + $exchangeTokenParameters = [ |
|
124 | + 'grant_type' => 'ig_exchange_token', |
|
125 | + 'client_secret' => $this->clientSecret, |
|
126 | + 'access_token' => $this->getStoredData('access_token'), |
|
127 | + ]; |
|
128 | + $url = 'https://graph.instagram.com/access_token'; |
|
129 | + } |
|
130 | + |
|
131 | + $response = $this->httpClient->request( |
|
132 | + $url, |
|
133 | + 'GET', |
|
134 | + $exchangeTokenParameters |
|
135 | + ); |
|
136 | + |
|
137 | + $this->validateApiResponse('Unable to exchange the access token'); |
|
138 | + |
|
139 | + $this->validateAccessTokenExchange($response); |
|
140 | + |
|
141 | + return $response; |
|
142 | + } |
|
143 | + |
|
144 | + /** |
|
145 | + * {@inheritdoc} |
|
146 | + */ |
|
147 | + public function getUserProfile() |
|
148 | + { |
|
149 | + $response = $this->apiRequest('me', 'GET', [ |
|
150 | + 'fields' => 'id,username,account_type,media_count', |
|
151 | + ]); |
|
152 | + |
|
153 | + $data = new Collection($response); |
|
154 | + if (!$data->exists('id')) { |
|
155 | + throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); |
|
156 | + } |
|
157 | + |
|
158 | + $userProfile = new User\Profile(); |
|
159 | + $userProfile->identifier = $data->get('id'); |
|
160 | + $userProfile->displayName = $data->get('username'); |
|
161 | + $userProfile->profileURL = "https://instagram.com/{$userProfile->displayName}"; |
|
162 | + $userProfile->data = [ |
|
163 | + 'account_type' => $data->get('account_type'), |
|
164 | + 'media_count' => $data->get('media_count'), |
|
165 | + ]; |
|
166 | + |
|
167 | + return $userProfile; |
|
168 | + } |
|
169 | + |
|
170 | + /** |
|
171 | + * Fetch user medias. |
|
172 | + * |
|
173 | + * @param int $limit Number of elements per page. |
|
174 | + * @param string $pageId Current pager ID. |
|
175 | + * @param array|null $fields Fields to fetch per media. |
|
176 | + * |
|
177 | + * @return \Hybridauth\Data\Collection |
|
178 | + * |
|
179 | + * @throws \Hybridauth\Exception\HttpClientFailureException |
|
180 | + * @throws \Hybridauth\Exception\HttpRequestFailedException |
|
181 | + * @throws \Hybridauth\Exception\InvalidAccessTokenException |
|
182 | + * @throws \Hybridauth\Exception\UnexpectedApiResponseException |
|
183 | + */ |
|
184 | + public function getUserMedia($limit = 12, $pageId = null, array $fields = null) |
|
185 | + { |
|
186 | + if (empty($fields)) { |
|
187 | + $fields = [ |
|
188 | + 'id', |
|
189 | + 'caption', |
|
190 | + 'media_type', |
|
191 | + 'media_url', |
|
192 | + 'thumbnail_url', |
|
193 | + 'permalink', |
|
194 | + 'timestamp', |
|
195 | + 'username', |
|
196 | + ]; |
|
197 | + } |
|
198 | + |
|
199 | + $params = [ |
|
200 | + 'fields' => implode(',', $fields), |
|
201 | + 'limit' => $limit, |
|
202 | + ]; |
|
203 | + if ($pageId !== null) { |
|
204 | + $params['after'] = $pageId; |
|
205 | + } |
|
206 | + |
|
207 | + $response = $this->apiRequest('me/media', 'GET', $params); |
|
208 | + |
|
209 | + $data = new Collection($response); |
|
210 | + if (!$data->exists('data')) { |
|
211 | + throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); |
|
212 | + } |
|
213 | + |
|
214 | + return $data; |
|
215 | + } |
|
216 | + |
|
217 | + /** |
|
218 | + * Fetches a single user's media. |
|
219 | + * |
|
220 | + * @param string $mediaId Media ID. |
|
221 | + * @param array|null $fields Fields to fetch per media. |
|
222 | + * |
|
223 | + * @return \Hybridauth\Data\Collection |
|
224 | + * |
|
225 | + * @throws \Hybridauth\Exception\HttpClientFailureException |
|
226 | + * @throws \Hybridauth\Exception\HttpRequestFailedException |
|
227 | + * @throws \Hybridauth\Exception\InvalidAccessTokenException |
|
228 | + * @throws \Hybridauth\Exception\UnexpectedApiResponseException |
|
229 | + */ |
|
230 | + public function getMedia($mediaId, array $fields = null) |
|
231 | + { |
|
232 | + if (empty($fields)) { |
|
233 | + $fields = [ |
|
234 | + 'id', |
|
235 | + 'caption', |
|
236 | + 'media_type', |
|
237 | + 'media_url', |
|
238 | + 'thumbnail_url', |
|
239 | + 'permalink', |
|
240 | + 'timestamp', |
|
241 | + 'username', |
|
242 | + ]; |
|
243 | + } |
|
244 | + |
|
245 | + $response = $this->apiRequest($mediaId, 'GET', [ |
|
246 | + 'fields' => implode(',', $fields), |
|
247 | + ]); |
|
248 | + |
|
249 | + $data = new Collection($response); |
|
250 | + if (!$data->exists('id')) { |
|
251 | + throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); |
|
252 | + } |
|
253 | + |
|
254 | + return $data; |
|
255 | + } |
|
256 | 256 | } |