我目前正在尝试将用户在我的网站(简单的 Php 和 Mysql)上使用的密码与我为他们创建的 Microsoft 帐户的密码同步。所以基本上他们都有一个带有随机密码的{user_id}@mydomain.com帐户,如果他们想使用它,他们基本上会在我网站客户区的专用页面上重新输入他们的密码,这样我就可以创建一个API 调用并用自己的方式更新随机密码。
- 我在没有登录用户的情况下工作(原因很明显)
- 我已正确设置所需的“应用程序权限”: User.ReadWrite.All、User.ManageIdentities.All、Directory.ReadWrite.All
但是,它说我没有足够的权限:
array(1) { ["error"]=> array(3) { ["code"]=> string(27) "Authorization_RequestDenied" ["message"]=> string(50) "Insufficient privileges to complete the operation." ["innerError"]=> array(2) { ["request-id"]=> string(36) "acb8b9c7-6a63-4157-8c92-de5f49a69ac8" ["date"]=> string(19) "2020-04-30T17:13:04" } } }
这是我目前用来更新密码的代码,以防万一:
$password = array(
'forceChangePasswordNextSignIn' => false,
'forceChangePasswordNextSignInWithMfa' => false,
'password' => $_POST['password']
);
$data = array(
'passwordProfile' => $password
);
$updatePassword = patch_microsoft_graph('users/'.$userID,$data,true);
我还留给您 patch_microsoft_graph() 函数的参考:
function patch_microsoft_graph($scope,$data,$object) {
$access_token = get_microsoft_access_token();
if(!$scope) {
return 'no $scope';
}
$payload = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://graph.microsoft.com/v1.0/$scope");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("application/x-www-form-urlencoded"));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$headers = [
"Authorization: $access_token",
"Host: graph.microsoft.com",
"Content-Type: application/json",
"Accept: application/json"
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$server_output = curl_exec($ch);
curl_close ($ch);
return json_decode($server_output,$object);
}
有人可以帮忙吗?提前致谢