You are here

public function AuthorizationCodeGrantService::getAccessToken in OAuth2 Client 8.3

Same name and namespace in other branches
  1. 8.2 src/Service/Grant/AuthorizationCodeGrantService.php \Drupal\oauth2_client\Service\Grant\AuthorizationCodeGrantService::getAccessToken()

Get an OAuth2 access token.

Parameters

string $pluginId: The plugin ID of the OAuth2 Client plugin for which an access token should be retrieved.

Overrides Oauth2ClientGrantServiceInterface::getAccessToken

File

src/Service/Grant/AuthorizationCodeGrantService.php, line 57

Class

AuthorizationCodeGrantService
Handles Authorization Grants for the OAuth2 Client module.

Namespace

Drupal\oauth2_client\Service\Grant

Code

public function getAccessToken($pluginId) {
  $provider = $this
    ->getProvider($pluginId);

  // Get the authorization URL. This also generates the state.
  $authorization_url = $provider
    ->getAuthorizationUrl();

  // Save the state to Drupal's tempstore.
  $this->tempstore
    ->set('oauth2_client_state-' . $pluginId, $provider
    ->getState());
  if ($this->currentRequest
    ->hasSession()) {

    // If we have a session, save before redirect.
    $this->currentRequest
      ->getSession()
      ->save();
  }

  // Redirect to the authorization URL.
  $redirect = new RedirectResponse($authorization_url);
  $redirect
    ->send();
  exit;
}