Microsoft graph 401 Unauthorized error while reque

2019-06-05 07:48发布

问题:

Trying request calendars list, but get 401 Unauthorized error. I can get users, can create new user, but can't get or create calendars.

I'm using these libraries:

thephpleague/oauth2-client

msgraph-sdk-php

get token via Client Credentials Grant method, access without a user.

$this->oauthClient = new \League\OAuth2\Client\Provider\GenericProvider([
            'clientId'                => $config['oauth_app_id'],
            'clientSecret'            => $config['oauth_app_password'],
            'redirectUri'             => $config['oauth_redirect_uri'],
            'urlAuthorize'            => $config['oauth_authority'].$config['tenantID'].$config['oauth_authorize_endpoint'],
            'urlAccessToken'          => $config['oauth_authority'].$config['tenantID'].$config['oauth_token_endpoint'],
            'urlResourceOwnerDetails' => '',
            'scopes'                  => $config['oauth_scopes']
        ]);

$newToken = $this->oauthClient->getAccessToken('client_credentials', [
                    'scope' => 'https://graph.microsoft.com/.default'
                ]);

$graph = new \Microsoft\Graph\Graph();
        $graph->setAccessToken($newToken);

//This works

$users = $graph->createRequest('GET', '/users/')
            ->setReturnType(\Microsoft\Graph\Model\User::class)
            ->execute();

And this works

$users = $graph->createRequest('GET', '/users/{user_id}')
            ->setReturnType(\Microsoft\Graph\Model\User::class)
            ->execute();

But this not works

 $calendars = $graph->createRequest('GET', '/users/{user_id}/calendars')
                ->setReturnType(\Microsoft\Graph\Model\Calendar::class)
                ->execute();

In apps.dev.microsoft.com and in Azure portal trying sets all permissions this not helps. In Graph Explorer same result.