0

I am having trouble authenticating and accessing the Microsoft Graph API via PHP.

I have the Azure app all setup with clientID and clientSecret codes, as well as the correct redirectURI.

When I try to view this PHP code in web browser, I get a HTTP 500 error.

I have the below code:

<?php
use Microsoft\Graph\Graph;
use Microsoft\Graph\Model;

$clientSecret = 'SAMPLE';
$clientId = 'SAMPLE';
$redirectUri = 'SAMPLE';
$tenantId = 'SAMPLE';

$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;

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

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

echo "<script type='text/javascript'>alert('$user->getGivenName()');</script>";

?>

I am hoping someone can provide anything useful, as I can't seem to find documentation anywhere on this.

I have also had to install composer.phar on web hosting via SSH, and am not sure if that is running correctly / in the correct directory. It does have all the required add-ins installed though such as guzzleHTTP, Microsoft Graph, and oauth2.

Webicex
  • 1
  • 1
  • I have now updated the post, thanks. In my real code it has the actual values with no syntax errors - however, so I still need to find the issue. – Webicex Jul 31 '20 at 10:51
  • Have you checked for errors as `500 Error` can be confusing sometime. E.g If you set php to log errors but not to display them, 500 get thrown when fatal error occurs during execution. https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display – Umair Khan Jul 31 '20 at 10:56

0 Answers0