You are here

public function AuthorizationCodeGrantService::requestAccessToken in OAuth2 Client 8.3

Executes an authorization_code grant request with the give code.

Parameters

string $pluginId: The client plugin id.

string $code: The authorization code.

Return value

bool Was a valid token retrieved?

Throws

\Drupal\oauth2_client\Exception\InvalidOauth2ClientException Exception thrown when trying to retrieve a non-existent OAuth2 Client.

File

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

Class

AuthorizationCodeGrantService
Handles Authorization Grants for the OAuth2 Client module.

Namespace

Drupal\oauth2_client\Service\Grant

Code

public function requestAccessToken($pluginId, $code) {
  $provider = $this
    ->getProvider($pluginId);

  // Try to get an access token using the authorization code grant.
  try {
    $accessToken = $provider
      ->getAccessToken('authorization_code', [
      'code' => $code,
    ]);
    if ($accessToken instanceof AccessTokenInterface) {
      $this
        ->storeAccessToken($pluginId, $accessToken);
      return TRUE;
    }
  } catch (IdentityProviderException $e) {
    watchdog_exception('OAuth2 Client', $e);
  }
  return FALSE;
}