You are here

public function ResourceOwnersCredentialsGrantService::getAccessToken in OAuth2 Client 8.2

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

Get an OAuth2 access token.

Parameters

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

Overrides Oauth2ClientGrantServiceInterface::getAccessToken

File

src/Service/Grant/ResourceOwnersCredentialsGrantService.php, line 15

Class

ResourceOwnersCredentialsGrantService
Handles Authorization Grants for the OAuth2 Client module.

Namespace

Drupal\oauth2_client\Service\Grant

Code

public function getAccessToken($clientId) {
  $provider = $this
    ->getProvider($clientId);
  $client = $this
    ->getClient($clientId);
  try {
    $accessToken = $provider
      ->getAccessToken('password', [
      'username' => $client
        ->getUsername(),
      'password' => $client
        ->getPassword(),
    ]);
    $this
      ->storeAccessToken($clientId, $accessToken);
  } catch (IdentityProviderException $e) {

    // Failed to get the access token.
    watchdog_exception('OAuth2 Client', $e);
  }
}