【发布时间】:2019-07-01 22:29:58
【问题描述】:
我想配置我的 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 中运行相同的查询。
这些是我授予应用的权限:
我应该怎么做才能克服上述问题?
【问题讨论】:
标签: php symfony microsoft-graph-api guzzle azure-ad-graph-api