Connecting to MS Graph through PHP

Bastian Rohde 0 Reputation points
2024-11-12T11:31:46.61+00:00

Hi,

I have tried various approaches now, but failed every time to establish a connection to MS Graph through a client secret..

There are two scenarios I'd like to cover:

  1. Send an email from a user mailbox
  2. Store and retrieve files from Sharepoint

I understand that I have to execute those under a permissioned user, so I would need to first fetch the user and then execute everything else from that user. However, I don't even get there right now... I have registered my application to have application level rights for User.ReadAll and Mail.Read / ReadWrite etc.

I managed to get an access token by calling the API directly via GuzzleHttp through some tutorial I found. However, that is then using the Graph class to execute further and it seems that does not exist in the PHP SDK, instead it is a GraphServiceClient

If I follow the instructions to use GraphServiceClient, I always get an "invalid scope" error. Here is how the code looks, what am I doing wrong??

use Microsoft\Kiota\Authentication\Oauth\ClientCredentialContext;
use Microsoft\Graph\Core\Authentication\GraphPhpLeagueAuthenticationProvider;

$tokenRequestContext = new ClientCredentialContext(
    'tenantId',
    'clientId',
    'clientSecret'
);

use Microsoft\Graph\GraphServiceClient;


// With specific scopes
$scopes = ['User.ReadAll'];
$graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);


try {
    $user = $graphServiceClient->users()->byUserId('UPN goes here')->get()->wait();
    echo "Hello, I am {$user->getGivenName()}";

} catch (ApiException $ex) {
    echo $ex->getError()->getMessage();
}
Microsoft 365 and Office SharePoint Development
Microsoft Security Microsoft Graph
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Anonymous
    2024-11-13T06:16:21.16+00:00

    Hello Bastian Rohde,

    Thank you for reaching out to Microsoft Support!

    Change the value of $scopes as follows:

    $scopes = ['https://graph.microsoft.com/.default'];
    

    And grant the application Application permission User.Read.All in Azure AD and grant the administrator consent as follows:

    User's image

    Reference document:

    https://learn.microsoft.com/en-us/graph/sdks/choose-authentication-providers?tabs=PHP#using-a-client-secret-3

    https://learn.microsoft.com/en-us/graph/api/user-get?view=graph-rest-1.0&tabs=php

    For operations such as sending mail and retrieving files from Sharepoint, you may need to grant more permissions to the app, depending on the document.

    Hope this helps.

    If the answer is helpful, please click Accept Answer and kindly upvote it. If you have any further questions about this answer, please click Comment.


  2. Bastian Rohde 0 Reputation points
    2024-11-23T22:43:56.8933333+00:00

    Hi, any further feedback on how to perform the activity of file upload using Graph SDK v2.18 which doesn’t contain the class Graph used in the tutorial?

    0 comments No comments

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.