You are here

public function GoogleApiClient::getAccessTokenByAuthCode in Google API PHP Client 8

Wrapper for Google_Client::fetchAccessTokenWithAuthCode.

Parameters

string $code: Code string from callback url.

Return value

array Token values array.

File

src/Service/GoogleApiClient.php, line 110

Class

GoogleApiClient
Class Google API Client Service.

Namespace

Drupal\google_api_client\Service

Code

public function getAccessTokenByAuthCode($code) {
  $token = $this->googleClient
    ->fetchAccessTokenWithAuthCode($code);
  if (isset($token['access_token'])) {
    $this
      ->setTokenCache('google_access_token', $token);
  }

  // Refresh token is only set the first time.
  if (isset($token['refresh_token'])) {
    $this
      ->setTokenCache('google_refresh_token', [
      $token['refresh_token'],
    ]);
  }
  return $token;
}