MNI Noyon
MNI Noyon

Reputation: 98

Microsoft Graph SDK for PHP - 400 bad request while trying to get data

I have tried a lot to find out my issue but no luck there. I am using Guzzle for my authentication and it's giving me the token. So there is no issue. The problem is while i am trying to get data it is showing me " An uncaught Exception was encountered Type: GuzzleHttp\Exception\ClientException

Message: Client error: GET https://graph.microsoft.com/v1.0/me resulted in a 400 Bad Request response: { "error": { "code": "BadRequest", "message": "Current authenticated context is not valid for this request. (truncated...) "

Here is my code below

$url = 'https://login.microsoftonline.com/' . $tenantId . '/oauth2/token?api-version=1.0';
    $token = json_decode($guzzle->post($url, [
        'form_params' => [
            'client_id' => $appId,
            'client_secret' => $appSecret,
            'resource' => 'https://graph.microsoft.com/',
            'grant_type' => 'client_credentials',
        ],
    ])->getBody()->getContents());
    $accessToken = $token->access_token;

    $graph = new Graph();
    $graph->setAccessToken($accessToken);

    $user = $graph->createRequest("GET", "/me")
        ->setReturnType(Model\User::class)
        ->execute();

    echo "Hello, I am $user->getGivenName() ";

Note: I have successfully got the token, and i am using CodeIgniter framework where CSRF is false. Also i have given the permission for the app User.Read.All, User.ReadWrite.All

Please help me out to solve of this problem. Thanks in advance.

Upvotes: 0

Views: 746

Answers (1)

Marc LaFleur
Marc LaFleur

Reputation: 33094

You cannot call /me using Client Credentials. The purpose of the Client Credentials is to authenticate an application without a user. Therefore there is no way for the API to translate which user you mean when you call /me.

Upvotes: 2

Related Questions