public function Oauth2ClientService::getAccessToken in OAuth2 Client 8.3
Same name and namespace in other branches
- 8.2 src/Service/Oauth2ClientService.php \Drupal\oauth2_client\Service\Oauth2ClientService::getAccessToken()
Obtains an existing or a new access token.
Parameters
string $pluginId: The Oauth2Client plugin id.
string $username: Optional - The username if needed by the grant type.
string $password: Optional - The password if needed by the grant type.
Return value
\League\OAuth2\Client\Token\AccessTokenInterface|null Returns a token or null.
Throws
\Drupal\oauth2_client\Exception\InvalidOauth2ClientException Thrown in the upstream League library.
File
- src/
Service/ Oauth2ClientService.php, line 80
Class
- Oauth2ClientService
- The OAuth2 Client service.
Namespace
Drupal\oauth2_client\ServiceCode
public function getAccessToken($pluginId, $username = '', $password = '') {
$accessToken = $this
->retrieveAccessToken($pluginId);
if ($accessToken instanceof AccessTokenInterface) {
$refreshToken = $accessToken
->getRefreshToken();
$expirationTimestamp = $accessToken
->getExpires();
if (!empty($expirationTimestamp) && $accessToken
->hasExpired() && !empty($refreshToken)) {
$accessToken = $this->grantServices['refresh_token']
->getAccessToken($pluginId);
}
}
else {
$client = $this
->getClient($pluginId);
switch ($client
->getGrantType()) {
case 'authorization_code':
$this->grantServices['authorization_code']
->getAccessToken($pluginId);
break;
case 'client_credentials':
$this->grantServices['client_credentials']
->getAccessToken($pluginId);
$accessToken = $this
->retrieveAccessToken($pluginId);
break;
case 'resource_owner':
$this->grantServices['resource_owner']
->setUsernamePassword($pluginId, $username, $password);
$this->grantServices['resource_owner']
->getAccessToken($pluginId);
$accessToken = $this
->retrieveAccessToken($pluginId);
break;
}
}
return $accessToken;
}