【发布时间】:2019-09-14 03:26:14
【问题描述】:
我正在构建一个具有简单电子邮件密码登录的应用程序。我想使用 Microsoft Graph 来返回已登录用户的个人资料信息(我现在没有使用 office365 OAuth)。
我设置了一个操作,试图从我所属的组织中获取此用户数据(即名字或个人资料照片)。我收到了这个错误:
"line":113, "message":"Client error: GET https://graph.microsoft.com/v1.0/users/{user} resulted in a 401 Unauthorized response: { "error": { "code": "Authorization_IdentityNotFound", "message": "The identity of the calling application (truncated...)" } }
我已经按照 Microsoft Graph 教程 (step 2) 中的说明设置了应用程序,这是我使用 repository readme 编写的代码:
class MsGraphService implements iAction
{
protected $accessToken;
public function __construct()
{
$guzzle = new \GuzzleHttp\Client();
$url = 'https://login.microsoftonline.com/'.ENV('MSGRAPH_TENAND_ID').'/oauth2/token?api-version=1.0';
$token = json_decode($guzzle->post($url, [
'form_params' => [
'client_id' => ENV('MSGRAPH_CLIENT_ID'),
'client_secret' => ENV('MSGRAHP_CLIENT_SECRET'),
'resource' => 'https://graph.microsoft.com/',
'grant_type' => 'client_credentials',
],
])->getBody()->getContents());
$this->accessToken = $token->access_token;
}
public function execute($data)
{
$graph = new Graph();
$graph->setAccessToken($this->accessToken);
$user = $graph->createRequest("GET", sprintf("/users/%s",$data['email']))
->setReturnType(Model\User::class)
->execute();
return $user->getGivenName();
}
}
accesstoken属性被抓到了,所以我觉得app配置没问题。
可能是关于 API 权限的问题,具体来说:应用程序权限?
我也询问过this on GitHub。
【问题讨论】:
-
您是否收到了您注册的应用程序的管理员同意书?此外,请记住,客户端凭据通常不是 PHP 的合适选择。客户端凭据应仅在服务器上使用,而不应在客户端上使用。
标签: php microsoft-graph-api microsoft-graph-sdks