简体   繁体   中英

Microsoft Graph in a LARAVEL PHP app - oauth2 grant method 'password'

i'am using a LARAVEL PHP application which is trying to get a token from Microsoft GRAPH API. Acutally, i can easily get a token by using the an OAuth2 Authorisation CODE grant method.

here is the way to do it : https://github.com/microsoftgraph/microsoft-graph-docs/blob/master/concepts/php.md

But using this method is redirecting the browser to a Microsoft authentication web page, and i don't want this. I would like a transparent method without any browser redirection.

Here are all the Oauth2 grant méthods to get a token : https://alexbilbie.com/guide-to-oauth-2-grants

So i want to get a token by using "Resource owner credentials grant". Here is my code :

    $provider = new \League\OAuth2\Client\Provider\GenericProvider([    
    'clientId'          => 'b9e6844b-b20c-4eef-bbff-fa052c1c0f94',
    'clientSecret'      => '1A8yUUcMaD5Zdv1dgwqy3SR',
    'redirectUri'       => 'https://my-website-url/oauth',
    'urlAuthorize'    => 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize',
    'urlAccessToken'    => 'https://login.microsoftonline.com/common/oauth2/v2.0/token',
    'urlResourceOwnerDetails'    => '',
    'scopes'                  => 'openid email profile' //  Admin restricted scopes : Directory.Read Directory.ReadWrite Groups.Read.All
]);

$accessToken = $provider->getAccessToken('password',[
    'username' => 'myaccount@assystem.com',
    'password' => 'xxxxx'
]);

But i got the following error :

(1/1) IdentityProviderException invalid_grant in GenericProvider.php (line 217)

Any help?

After getting an error exception, i know why i got this error :

    try {
    // Try to get an access token using the resource owner password credentials grant.
    $accessToken = $provider->getAccessToken('password', [
        'username' => 'myapp@assystem.com',
        'password' => 'mypassword'
    ]);
} catch (\League\OAuth2\Client\Provider\Exception\IdentityProviderException $e) {

    // Failed to get the access token
    throw $e;
}

'error_description' => 'AADSTS70000: The grant is not supported by this API version ...(Microsoft Graph API)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM