PHP connection to microsoft Graph SDK

Daniel Bäuerlein 1 Reputation point
2022-01-18T09:50:19.053+00:00

Hi all,

I would like to use PHP to access various areas of Outlook 365 (including retrieving emails, retrieving/creating calendar entries, etc).

"Previously" I did this via PHP-EWS. But apparently support for PHP-EWS will be discontinued by Microsoft at the end of the year.
So now I want to switch to the Microsoft Graph API.

I installed this SDK via Composer:
https://github.com/microsoftgraph/msgraph-sdk-php

Then I registered my application via Microsoft Azure Active Directory:
https://portal.azure.com/#blade/Micr...tionsListBlade

There I got my tenantId, ClientID, and the clientSecret.

Now I wanted to get my first "Hello World" experience and failed - how should it be different ;-( .

Can you help me to get this first piece of code working?

require_once DIR . '/vendor/autoload.php';

// Include the Microsoft Graph classes
use Microsoft\Graph\Graph;
use Microsoft\Graph\Model;

// Data from Azure Active Diretory
$tenantId="xxx";
$clientId="yyy";
$clientSecret="zzz";

$guzzle = new \GuzzleHttp\Client();
$url = 'https://login.microsoftonline.com/' . $tenantId . '/oauth2/token?api-version=1.0';
$token = json_decode($guzzle->post($url, [
'form_params' => [
'client_id' => $clientId,
'client_secret' => $clientSecret,
'resource' => 'https://graph.microsoft.com/',
'grant_type' => 'client_credentials',
],
])->getBody()->getContents());

$accessToken = $token->access_token;

// This works! The Access-Token is echoed
echo "AccessToken:".$accessToken;

// But from here on, i get no output
$graph = new Graph();
$graph->setAccessToken($accessToken);

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

print_r($user);

echo "Hello, my name is {$user->getGivenName()}.";

Furthermore i wonder, if this is the correct way to connect or if i should use api-version 2.0?

$url = 'https://login.microsoftonline.com/' . $tenantId . '/oauth2/token?api-version=1.0';

Would that be the correct URL? But if i use this, i don't get an accessToken

$url = 'https://login.microsoftonline.com/' . $tenantId . '/oauth2/v2.0/token?api-version=2.0';

Thank you for your support.

Best wishes
Daniel

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,645 questions
{count} votes

1 answer

Sort by: Most helpful
  1. CarlZhao-MSFT 37,216 Reputation points
    2022-01-19T09:33:39.993+00:00

    Hi @Daniel Bäuerlein

    Since your script is using the client credential flow, you cannot call the /me endpoint. You need to grant the User.Read.All application permission to the application, then call the /users/{user id} endpoint.

    166304-image.png

    require_once DIR . '/vendor/autoload.php';  
      
    // Include the Microsoft Graph classes  
    use Microsoft\Graph\Graph;  
    use Microsoft\Graph\Model;  
      
    // Data from Azure Active Diretory  
    $tenantId="xxx";  
    $clientId="yyy";  
    $clientSecret="zzz";  
      
    $guzzle = new \GuzzleHttp\Client();  
    $url = 'https://login.microsoftonline.com/' . $tenantId . '/oauth2/token?api-version=1.0';  
    $token = json_decode($guzzle->post($url, [  
    'form_params' => [  
    'client_id' => $clientId,  
    'client_secret' => $clientSecret,  
    'resource' => 'https://graph.microsoft.com/',  
    'grant_type' => 'client_credentials',  
    ],  
    ])->getBody()->getContents());  
      
    $accessToken = $token->access_token;  
      
    // This works! The Access-Token is echoed  
    echo "AccessToken:".$accessToken;  
      
    // But from here on, i get no output  
    $graph = new Graph();  
    $graph->setAccessToken($accessToken);  
      
    $user = $graph->createRequest("GET", "/users/{user id}")  
    ->setReturnType(Model\User::class)  
    ->execute();  
      
    print_r($user);  
      
    echo "Hello, my name is {$user->getGivenName()}.";  
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.