我正在使用 Microsoft graph
用于更新用户密码的API。我有以下 json 请求:
{
"accountEnabled": true,
"userPrincipalName": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e89c8d9b9c9d9b8d9adbdca885919c8d8689869c8689858dc6878685818b9a879b878e9cc68b8785" rel="noreferrer noopener nofollow">[email protected]</a>",
"passwordProfile": {
"forceChangePasswordNextSignIn": false,
"password": "<new_password>"
}
}
我称之为url
https://graph.microsoft.com/v1.0/users/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0c78697f78797f697e3f384c61757869626d6278626d616922636261656f7e637f636a78226f6361" rel="noreferrer noopener nofollow">[email protected]</a>
使用PATCH
方法。我还将不记名 token 作为授权 header 传递,但出现以下错误:
{
"error": {
"code": "Authorization_RequestDenied",
"message": "Insufficient privileges to complete the operation.",
"innerError": {
"request-id": "12781f2a-0cdd-449d-94d7-aae1440a7559",
"date": "2020-04-03T02:52:57"
}
}
}
错误提示Insufficient privileges
。根据这个page我正在关注,它说:
When updating the passwordProfile property, the following permission is required: Directory.AccessAsUser.All.
我已经在创建的应用程序中检查了这一点,并且该权限位于应用程序注册中
但我仍然收到此错误。任何人都可以帮助解决这个错误。谢谢
编辑:也在 Microsoft 中添加了权限,但仍然收到相同的错误
编辑2:
我有下面的代码,我可以从中获取 token :
data = {
"grant_type": "client_credentials",
"client_secret": <client_secret>,
"client_id": <client_id>,
"resource": "https://graph.microsoft.com"
}
r = requests.post("https://login.microsoftonline.com/<tennant_id>/oauth2/token", data)
if r.status_code == 200:
ret_body = r.json()
token = ret_body['access_token']
else:
log.error("Unable to get token from oauth {}, {}".format(r.status_code, r.json()))
编辑3:
我还在用户管理员中添加了应用程序名称:
但是问题还是一样。
最佳答案
您正在使用 Microsoft Graph
,因此您需要在 Microsoft Graph
而不是 Azure Active Directory Graph
中添加权限。
更新:
您正在使用的是 client credentials flow ,使用此流程时,需要Application权限
,如果想要获取token来更新passwordProfile
,则委托(delegate)权限 需要Directory.AccessAsUser.All
,要更新用户的Application权限
,最多可以添加Directory.ReadWrite.All
,但是这个权限不能重置用户密码
。
检查permissions从最低特权到最高特权:
然后查看Remarks对于Directory.ReadWrite.All
:
解决方案:
要使用客户端凭据流更新 passwordProfile
,请将 AD 应用的服务主体添加为 Azure AD 中的目录角色。
导航到门户中的 Azure Active Directory
-> 角色和管理员
-> 用户管理员
-> 添加分配
-> 搜索您的 AD 应用程序的名称 -> 添加
。
然后获取token来调用API,就可以正常工作了:
关于azure - 如何使用graph api更新azure ad中的用户密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61004464/