You are here

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

Same name and namespace in other branches
  1. 8.2 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.

Parameters

\Google_Client|null $client: Optionally parameter for developers who want to set initial google client object.

Return value

\Google_Client|bool Google_Client object with all params from the account or false.

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 131

Class

GoogleApiClientService
Class Google API Client Service.

Namespace

Drupal\google_api_client\Service

Code

private function getClient(\Google_Client $client = NULL) {
  if (!google_api_client_load_library()) {

    // We don't have library installed notify admin and abort.
    $status_report_link = Link::createFromRoute($this
      ->t('Status Report'), 'system.status')
      ->toString();
    $this->messenger
      ->addError($this
      ->t("Can't get the google client as library is missing check %status_report for more details. Report this to site administrator.", [
      '%status_report' => $status_report_link,
    ]));
    $response = new RedirectResponse('<front>');
    $response
      ->send();
    return FALSE;
  }
  if ($client == NULL) {
    $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
      ->setApprovalPrompt('force');
  }
  $client
    ->setClientSecret($google_api_client
    ->getClientSecret());
  $client
    ->setDeveloperKey($google_api_client
    ->getDeveloperKey());
  $client
    ->setRedirectUri(google_api_client_callback_url());
  $client
    ->setApplicationName($google_api_client
    ->getName());
  $scopes = $google_api_client
    ->getScopes();

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