You are here

private function GoogleApiServiceClientService::getClient in Google API PHP Client 8.3

Same name and namespace in other branches
  1. 8.4 src/Service/GoogleApiServiceClientService.php \Drupal\google_api_client\Service\GoogleApiServiceClientService::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

\Google_Exception|\Drupal\Core\Entity\EntityStorageException Google Exception if any api function fails and EntityStorage Exception if entity save fails.

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

File

src/Service/GoogleApiServiceClientService.php, line 122

Class

GoogleApiServiceClientService
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
    ->setAuthConfig($this->googleApiServiceClient
    ->getAuthConfig());
  $client
    ->setScopes($this->googleApiServiceClient
    ->getScopes(TRUE));
  if ($access_token = $this->googleApiServiceClient
    ->getAccessToken()) {
    $client
      ->setAccessToken($access_token);
    if ($client
      ->isAccessTokenExpired()) {
      $access_token = $client
        ->fetchAccessTokenWithAssertion();
      if ($access_token) {
        $this->googleApiServiceClient
          ->setAccessToken($access_token);
        $this->googleApiServiceClient
          ->save();
      }
    }
  }
  else {
    $access_token = $client
      ->fetchAccessTokenWithAssertion();
    if ($access_token) {
      $this->googleApiServiceClient
        ->setAccessToken($access_token);
      $this->googleApiServiceClient
        ->save();
    }
  }
  $this->googleClient = $client;
  return $client;
}