You are here

private function GoogleApiClient::setAccessToken in Google API PHP Client 8

Wrapper for Google_Client::setAccessToken.

Return value

bool Was the token added or not?

1 call to GoogleApiClient::setAccessToken()
GoogleApiClient::__construct in src/Service/GoogleApiClient.php
Callback Controller constructor.

File

src/Service/GoogleApiClient.php, line 194

Class

GoogleApiClient
Class Google API Client Service.

Namespace

Drupal\google_api_client\Service

Code

private function setAccessToken() {

  // Set whatever token is in cache. So we can check its validity!
  $accessTokenCache = $this
    ->getTokenCache('google_access_token');

  // If there was something in cache.
  if (!empty($accessTokenCache)) {
    $this->googleClient
      ->setAccessToken($accessTokenCache);

    // Check if the current cached token is expired?
    if ($this->googleClient
      ->isAccessTokenExpired()) {

      // Refresh the access token using refresh token.
      $tokenUpdated = $this
        ->getAccessTokenWithRefreshToken();

      // Now that there is a new access token in cache,
      // set it into the client.
      if ($tokenUpdated != FALSE) {
        $this->googleClient
          ->setAccessToken($tokenUpdated);

        // There should be a new unexpired token.
        return TRUE;
      }

      // Unable to update token.
      return FALSE;
    }

    // Token is set and is valid.
    return TRUE;
  }

  // There is no token cache.
  return FALSE;
}