protected function InstagramAccount::setNewToken in Instagram Feeds 8
Obtains short-lived token and exchanges it to long-lived one.
Parameters
string $client_id: Instagram Client (App) ID.
string $client_secret: Instagram Client (App) Secret.
string $code: Instagram Auth code.
bool $save: Save entity or not after token has been refreshed successfully.
Return value
bool TRUE if success, FALSE otherwise.
1 call to InstagramAccount::setNewToken()
- InstagramAccount::getToken in src/
Entity/ InstagramAccount.php - Gets the Instagram long-lived token.
File
- src/
Entity/ InstagramAccount.php, line 227
Class
- InstagramAccount
- Defines the instagram_account entity class.
Namespace
Drupal\instagram_feeds\EntityCode
protected function setNewToken($client_id, $client_secret, $code, $save = FALSE) : bool {
$httpClient = \Drupal::httpClient();
// Obtain short-live token (valid 24 hours).
$response = $httpClient
->post(self::INSTAGRAM_API_ENDPOINT . '/oauth/access_token', [
'form_params' => [
'client_id' => $client_id,
'client_secret' => $client_secret,
'grant_type' => 'authorization_code',
'redirect_uri' => Url::fromRoute('entity.instagram_account.add_form')
->setAbsolute()
->toString(),
'code' => $code,
],
]);
$body = $this
->extractInstagramData($response);
$this
->set('iid', $body['user_id']);
// Exchange short-term token to long-lived one (valid for 60 days).
$response = $httpClient
->get(self::INSTAGRAM_GRAPH_ENDPOINT . '/access_token?' . http_build_query([
'grant_type' => 'ig_exchange_token',
'client_secret' => $client_secret,
'access_token' => $body['access_token'],
]));
$body = $this
->extractInstagramData($response);
$this
->set('token_expiration', $body['expires_in'] + \Drupal::time()
->getRequestTime());
$this
->set('token', $body['access_token']);
if (!$this
->isNew() && $save) {
$this
->save();
}
return TRUE;
}