MS Graph PHP SDK fails to retrieve an access token

Ian Foulds 6 Reputation points
2024-03-13T13:26:25.3933333+00:00

I am using the PHP microsoft/microsoft-graph package version 2.3.0, PHP version 8.2.5.

Following the examples described here: https://learn.microsoft.com/en-us/graph/sdks/create-client?tabs=php#tabpanel_1_php

My code fails when it tries to fetch the token with a malformed request. Only the tenant ID is put into the /token endpoint request, the client_id and other required parameters are not being set on the oauthProvider within PhpLeagueAccessTokenProvider.

Here is my code, as per the example:


        $tokenRequestContext = new AuthorizationCodeContext(
            $this->tenantId,
            $this->clientId,
            $this->clientSecret,
            $this->getAccessToken()->__toString(),	// Actual token or 'AUTH_CODE_FROM_REDIRECT'
            $this->redirectUrl,
        );

        $provider = new GraphServiceClient($tokenRequestContext, self::AZURE_SCOPES);
        $provider->me()->get()->wait();

The nesting of calls ending up in PhpLeagueAccessTokenProvider do not set the required parameters in the oauthProvider on line 94:

        $this->oauthProvider = $oauthProvider ?? ProviderFactory::create($tokenRequestContext);

This merely sets the URLs and does not set the credentials within the provider resulting in an invalid request when calling the /token endpoint.

It looks like I am setting this up correctly according to all the documentation I've read, but the client credentials are just not being set in the requests.

Microsoft Security Microsoft Graph
0 comments No comments
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.