我想将我的 Symfony4 应用程序配置为使用msgraph-sdk-php库读取和发送电子邮件。
我的应用程序将从单个帐户读取和发送电子邮件,我不想将其密码暴露给我的应用程序用户。因此,我不会使用 OAuth 进行登录。
我的第一次体验是这段代码(用于检索邮箱用户配置文件):
<?php
namespace App\Graph;
use Microsoft\Graph\Exception\GraphException;
use Microsoft\Graph\Graph;
use Microsoft\Graph\Model\User;
class GraphService
{
function sentTestMessage() {
$userId = "************************************";
$tenantId = "************************************";
$clientId = "************************************";
$clientSecret = "***************************";
$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=new \stdClass();
try {
$user = $graph->createRequest("GET", "/users/".$userId)
->setReturnType(User::class)
->execute();
} catch (GraphException $e) {
$user->getGivenName=$e->getMessage();
}
return "Hello, I am $user->getGivenName() ";
}
}
但是随后 Symfony 向我显示了一个带有此消息的异常页面:
客户端错误:GET https://graph.microsoft.com/v1.0/users/...导致403 Forbidden响应:
{
“错误”: {
"code": "Authorization_RequestDenied",
"message": "权限不足,无法完成操作(已截断...)
现在,在同一用户登录的https://developer.microsoft.com/en-us/graph/graph-explorer 中运行时,相同的查询有效。
这些是我给应用程序的权限:
我应该怎么做才能克服上述问题?
呼啦一阵风