Hey,

I've now resorted into using a workaround, but here is something that would be nice to get working properly using oauth2_client. I was trying to create Oauth2Client plugin for Microsoft Graph client_credentials grant type but something is not right. Here is couple of code samples to illustrate my problem.

Here is reference code, from https://docs.microsoft.com/en-us/graph/sdks/create-client?tabs=PHP

// PHP client currently doesn't have an authentication provider. You will need to handle
// getting an access token. The following example demonstrates the client credential
// OAuth flow and assumes that an administrator has consented to the application.
$guzzle = new \GuzzleHttp\Client();
$url = 'https://login.microsoftonline.com/' . $tenantId . '/oauth2/token?api-version=1.0';
$token = json_decode($guzzle->post($url, [
    'form_params' => [
        'client_id' => $clientId,
        'client_secret' => $clientSecret,
        'resource' => 'https://graph.microsoft.com/',
        'grant_type' => 'client_credentials',
    ],
])->getBody()->getContents());
$accessToken = $token->access_token;

and that reference code is working for me in my tests.

If I try to get that $accessToken string with Oauth2Client plugin, like

$token = \Drupal::service('oauth2_client.service')->getAccessToken('MY_PLUGIN');
$accessToken = $token->getToken();

I do get a string token out from that, but it is not working one.

If I take all parameters from MY_PLUGIN and use it with the reference code, like

$guzzle = new \GuzzleHttp\Client();
$o365 = \Drupal::service('oauth2_client.service')->getClient('MY_PLUGIN');
$token = json_decode($guzzle->post($o365->getTokenUri(), [
  'form_params' => [
    'client_id' => $o365->getClientId(),
    'client_secret' => $o365->getClientSecret(),
    'resource' => $o365->getAuthorizationUri(),
    'grant_type' => $o365->getGrantType(),
  ],
])->getBody()->getContents());
$accessToken = $token->access_token;

I once again have a working token.

Any ideas what I may be missing?

Comments

mikran created an issue. See original summary.

mikran’s picture

Issue summary: View changes
fathershawn’s picture

Assigned: Unassigned » fathershawn

@mikran First, I'm sorry this question missed my inbox somehow! I'll try to see if something is off in the project settings.

Although this issue is filed against 3.x I'm wondering if you are using 3.x code? I ask because

$token = \Drupal::service('oauth2_client.service')->getAccessToken('MY_PLUGIN');

Would first trigger an attempt to retrieve the token from storage, and in 3.x we leave storage implementation to the plugin with two examples as the storage system chosen depends on the use case for the site. If it is 3.x, how are your storing your tokens?

mikran’s picture

Thanks for your reply @FatherShawn. It is 3.x (3.0.0-beta1) and I'm storing tokens to state similarly to authcode_example plugin.

fabianderijk’s picture

You can also take a look at the o365 module: https://www.drupal.org/project/o365

fathershawn’s picture

Status: Active » Closed (duplicate)

Closing in favor of #3256272: Additional parameters to get Access token - please see if the additional feature added there fixes your use case.