如何使用图形API更新Azure广告中的用户密码

时间:2020-04-03 03:02:53

标签: azure api graph passwords azure-active-directory

我正在使用Microsoft graph API更新用户密码。我有以下json请求:

{
    "accountEnabled": true,
    "userPrincipalName": "testuser34@mytenantname.onmicrosoft.com",
    "passwordProfile": {
        "forceChangePasswordNextSignIn": false,
        "password": "<new_password>"
    }
}

我正在使用url方法将此称为https://graph.microsoft.com/v1.0/users/testuser34@mytenantname.onmicrosoft.com PATCH。我还将传递承载令牌作为Authorization标头,但出现以下错误:

{
  "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.

我已经在创建的应用中检查了此内容,并且该许可在应用注册中存在

enter image description here

但是我仍然收到此错误。任何人都可以帮助解决该错误。谢谢

编辑:在Microsoft中也添加了权限,但仍然出现相同的错误

enter image description here

编辑2:

我有以下代码可用来获取令牌:

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()))

Edit3:

我还在用户管理员中添加了应用名称:

enter image description here

但是问题仍然存在。

1 个答案:

答案 0 :(得分:1)

您正在使用Microsoft Graph,因此需要在Microsoft Graph中而不是Azure Active Directory Graph中添加权限。 enter image description here

更新

您正在使用的是client credentials flow,在使用此流程时,需要Application permission,如果要获取令牌来更新passwordProfile,则需要 Delegated权限 Directory.AccessAsUser.All是必需的,要用Application permission更新用户,我们最多只能添加Directory.ReadWrite.All,但此权限不能reset user passwords

从最低特权到最高特权检查permissions

enter image description here

然后看到Directory.ReadWrite.All的{​​{3}}:

Remarks

解决方案:

要使用客户端凭据流更新passwordProfile,请将AD App的服务主体添加为Azure AD中的目录角色。

导航到门户网站中的Azure Active Directory-> Roles and administrators-> User administrator-> Add assignments->搜索广告应用程序的名称-> {{1 }}。

然后获取令牌以调用API,它可以正常工作:

Directory.ReadWrite.All