public function Callback::callbackUrl in Google API PHP Client 8.2
Same name and namespace in other branches
- 8.4 src/Controller/Callback.php \Drupal\google_api_client\Controller\Callback::callbackUrl()
 - 8 src/Controller/Callback.php \Drupal\google_api_client\Controller\Callback::callbackUrl()
 - 8.3 src/Controller/Callback.php \Drupal\google_api_client\Controller\Callback::callbackUrl()
 
Callback URL for Google API Auth.
Parameters
\Symfony\Component\HttpFoundation\Request $request: Request.
Return value
array Return markup for the page.
1 string reference to 'Callback::callbackUrl'
File
- src/
Controller/ Callback.php, line 54  
Class
- Callback
 - Google Client Callback Controller.
 
Namespace
Drupal\google_api_client\ControllerCode
public function callbackUrl(Request $request) {
  $account_id = $request
    ->get('id');
  $entity_type = $request
    ->get('type');
  if ($entity_type) {
    $_SESSION['google_api_client_account_type'] = $entity_type;
  }
  else {
    if (isset($_SESSION['google_api_client_account_type'])) {
      $entity_type = $_SESSION['google_api_client_account_type'];
    }
    else {
      $entity_type = 'google_api_client';
    }
  }
  google_api_client_load_library();
  if (!class_exists('Google_Client')) {
    \Drupal::messenger()
      ->addError(t("Can't authenticate with google as library is missing check Status report or Readme for requirements"));
    return new RedirectResponse('/admin/config/services/google_api_client');
  }
  if ($account_id == NULL && isset($_SESSION['google_api_client_account_id'])) {
    $account_id = $_SESSION['google_api_client_account_id'];
  }
  elseif ($account_id) {
    $_SESSION['google_api_client_account_id'] = $account_id;
  }
  if ($account_id) {
    $google_api_client = \Drupal::entityTypeManager()
      ->getStorage($entity_type)
      ->load($account_id);
    $this->googleApiClient
      ->setGoogleApiClient($google_api_client);
    $this->googleApiClient->googleClient
      ->setApplicationName("Google OAuth2");
    if ($request
      ->get('code')) {
      $this->googleApiClient->googleClient
        ->fetchAccessTokenWithAuthCode($request
        ->get('code'));
      $google_api_client
        ->setAccessToken(json_encode($this->googleApiClient->googleClient
        ->getAccessToken()));
      $google_api_client
        ->setAuthenticated(TRUE);
      $google_api_client
        ->save();
      unset($_SESSION['google_api_client_account_id']);
      $response = new RedirectResponse('/admin/config/services/google_api_client');
      \Drupal::messenger()
        ->addMessage(t('Api Account saved'));
      return $response;
    }
    if ($this->googleApiClient->googleClient) {
      $auth_url = $this->googleApiClient->googleClient
        ->createAuthUrl();
      $response = new TrustedRedirectResponse($auth_url);
      $response
        ->send();
    }
  }
  // Let other modules act of google response.
  \Drupal::moduleHandler()
    ->invokeAll('google_api_client_google_response', [
    $request,
  ]);
  $response = new RedirectResponse('/admin/config/services/google_api_client');
  return $response;
}