You are here

private function GoogleApiClientService::getClient in Google API PHP Client 8.2

Same name and namespace in other branches
  1. 8.4 src/Service/GoogleApiClientService.php \Drupal\google_api_client\Service\GoogleApiClientService::getClient()
  2. 8.3 src/Service/GoogleApiClientService.php \Drupal\google_api_client\Service\GoogleApiClientService::getClient()

Function to retrieve the google client for different operations.

Developers can pass the google_api_client object to setGoogleApiClient and get the api client ready for operations.

Return value

\Google_Client Google_Client object with all params from the account.

Throws

\Drupal\Core\Entity\EntityStorageException

1 call to GoogleApiClientService::getClient()
GoogleApiClientService::setGoogleApiClient in src/Service/GoogleApiClientService.php
Function to set the GoogleApiClient account for the service.

File

src/Service/GoogleApiClientService.php, line 84

Class

GoogleApiClientService
Class Google API Client Service.

Namespace

Drupal\google_api_client\Service

Code

private function getClient() {
  google_api_client_load_library();
  $client = new Google_Client();
  $client
    ->setRedirectUri(google_api_client_callback_url());
  if ($this->googleApiClient == NULL) {
    return $client;
  }
  $google_api_client = $this->googleApiClient;
  $client
    ->setClientId($google_api_client
    ->getClientId());
  if ($google_api_client
    ->getAccessType()) {
    $client
      ->setAccessType('offline');
  }
  $client
    ->setClientSecret($google_api_client
    ->getClientSecret());
  $client
    ->setDeveloperKey($google_api_client
    ->getDeveloperKey());
  $client
    ->setRedirectUri(google_api_client_callback_url());
  if ($google_api_client
    ->getAccessType()) {
    $client
      ->setApprovalPrompt('force');
  }
  $client
    ->setApplicationName("Google OAuth2");
  $scopes = $google_api_client
    ->getScopes();

  // Let other modules change scopes.
  $google_api_client_id = $google_api_client
    ->getId();
  \Drupal::moduleHandler()
    ->alter('google_api_client_account_scopes', $scopes, $google_api_client_id);
  $client
    ->addScope($scopes);
  $this->googleClient = $client;
  $this->googleClient
    ->setAccessToken($google_api_client
    ->getAccessToken());
  $this
    ->setAccessToken();
  return $client;
}