You are here

public function ResourceOwnersCredentialsGrantService::getAccessToken in OAuth2 Client 8.3

Same name and namespace in other branches
  1. 8.2 src/Service/Grant/ResourceOwnersCredentialsGrantService.php \Drupal\oauth2_client\Service\Grant\ResourceOwnersCredentialsGrantService::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/ResourceOwnersCredentialsGrantService.php, line 20

Class

ResourceOwnersCredentialsGrantService
Handles Authorization Grants for the OAuth2 Client module.

Namespace

Drupal\oauth2_client\Service\Grant

Code

public function getAccessToken($pluginId) {
  $provider = $this
    ->getProvider($pluginId);
  $credentials = $this
    ->getUsernamePassword($pluginId);
  if (empty($credentials)) {
    throw new \RuntimeException('Missing username and password for client plugin ' . $pluginId);
  }
  try {
    $accessToken = $provider
      ->getAccessToken('password', [
      'username' => $credentials['username'],
      'password' => $credentials['password'],
    ]);
    $this
      ->storeAccessToken($pluginId, $accessToken);
  } catch (\Exception $e) {

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