Skip to content

Instantly share code, notes, and snippets.

@isvargasmsft
Created October 10, 2023 17:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isvargasmsft/c9f674d60cb0ffdd502e216714fbc171 to your computer and use it in GitHub Desktop.
Save isvargasmsft/c9f674d60cb0ffdd502e216714fbc171 to your computer and use it in GitHub Desktop.
PHP simple initialization
<?php
use Microsoft\Graph\GraphServiceClient;
use Microsoft\Kiota\Abstractions\ApiException;
use Microsoft\Kiota\Authentication\Oauth\ClientCredentialContext;
set_include_path(__DIR__);
require 'vendor/autoload.php';
$tokenRequestContext = new ClientCredentialContext(
'tenantID',
'clientID',
'clientSecret'
);
$graphServiceClient = new GraphServiceClient($tokenRequestContext);
//Get a user
try {
$response = $graphServiceClient->users()->ByUserId('AlexW@M365x86781558.OnMicrosoft.com')->get()->wait();
echo "Hello, I am {$response->getGivenName()}";
} catch (ApiException $ex) {
echo $ex->getMessage();
}
<?php
use Microsoft\Graph\GraphServiceClient;
use Microsoft\Kiota\Abstractions\ApiException;
use Microsoft\Kiota\Authentication\Oauth\AuthorizationCodeContext;
set_include_path(__DIR__);
require 'vendor/autoload.php';
$tokenRequestContext = new AuthorizationCodeContext(
'tenantID',
'clientID',
'value',
'authCode',
'redirectURI'
);
$scopes = ['User.Read'];
$graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);
try {
$user = $graphServiceClient->me()->get()->wait();
echo "Hello, I am {$user->getGivenName()}";
} catch (ApiException $ex) {
echo $ex->getMessage();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment