首页 > 解决方案 > PHP Office 365 图形认证

问题描述

我在通过 PHP 验证和访问 Microsoft Graph API 时遇到问题。

我已经使用 clientID 和 clientSecret 代码以及正确的 redirectURI 设置了 Azure 应用程序。

当我尝试在 Web 浏览器中查看此 PHP 代码时,我收到 HTTP 500 错误。

我有以下代码:

<?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>";

?>

我希望有人可以提供任何有用的东西,因为我似乎在这方面的任何地方都找不到文档。

我还必须通过 SSH 在虚拟主机上安装 composer.phar,并且不确定它是否在正确的目录中正确运行。它确实安装了所有必需的插件,例如 guzzleHTTP、Microsoft Graph 和 oauth2。

标签: phphtmlauthenticationoffice365single-sign-on

解决方案


推荐阅读