You are here

public function Oauth::getAccessToken in Media: Acquia DAM 8

Get a token for API access + the number of seconds till expiration.

Parameters

string $auth_code: The authorization token from oauth.

Return value

array Returns an array with three keys:

  • access_token: The access token used for API authorization.
  • expire_time: The unix timestamp when the access token expires.
  • refresh_token: The refresh token used for API authorization.

Overrides OauthInterface::getAccessToken

File

src/Oauth.php, line 115

Class

Oauth
OAuth Class.

Namespace

Drupal\media_acquiadam

Code

public function getAccessToken($auth_code) {
  $this->loggerChannel
    ->debug('Getting new access token for @username.', [
    '@username' => $this->currentUser
      ->getAccountName(),
  ]);

  /** @var \Psr\Http\Message\ResponseInterface $response */
  $response = $this->httpClient
    ->post("{$this->damApiBase}/oauth2/token", [
    'form_params' => [
      'grant_type' => 'authorization_code',
      'code' => $auth_code,
      'redirect_uri' => $this->urlGenerator
        ->generateFromRoute('media_acquiadam.auth_finish', [
        'auth_finish_redirect' => $this->authFinishRedirect,
      ], [
        'absolute' => TRUE,
      ]),
      'client_id' => $this->config
        ->get('client_id'),
      'client_secret' => $this->config
        ->get('secret'),
    ],
  ]);
  $body = (string) $response
    ->getBody();
  $body = json_decode($body);
  return [
    'access_token' => $body->access_token,
    'expire_time' => time() + $body->expires_in,
    'refresh_token' => $body->refresh_token,
  ];
}