我正在尝试使用Microsoft Graph API更新Active Directory中的其他用户 .
我在https://apps.dev.microsoft.com/为用户和应用程序设置了以下权限
我已经请求了以下范围:
-
Directory.ReadWrite.All
-
User.ReadWrite.All
-
Group.ReadWrite.All
我能够获得目录中所有用户的列表,但在尝试更新时(在这种情况下, city
),它会失败:
GraphServiceClient graphClient = SDKHelper.GetAuthenticatedClient ();
var usersResponse = graphClient.Users.Request ().GetAsync ();
var users = usersResponse.Result;
// hard coding user id for now
var userId = "9a5b83cd-85ff-4ad1-ab2f-b443941a518e";
var user = users.FirstOrDefault (m => m.Id == userId);
if (user != null) {
user.City = "New York";
await graphClient.Me.Request ().UpdateAsync (user);
}
我明白了:
{
Code : Authorization_RequestDenied
Message : Insufficient privileges to complete the operation.
Inner error
}
我登录的用户是目录的全局管理员 .
我拿了JWT令牌,前往https://jwt.io,这些是我看到的角色:
-
Directory.Read.All
-
Directory.ReadWrite.All
-
Files.ReadWrite
-
Group.ReadWrite.All
-
Mail.Send
-
User.Read
-
User.Read.All
-
User.ReadWrite.All
我是否需要其他权限才能实现此目的?
在一天结束时,我想创建一个控制台应用程序(而不是Web应用程序),我可以更新目录中的其他用户信息 . 但我认为使用微软提供的这个示例应用程序是一个良好的开端 .
1 回答
您're seeing this is because you'传递完整
user
对象而不是仅传递city
属性的原因 . 换句话说,您正在尝试更新该user
记录中的每个属性,包括几个只读属性 .这是包含REST API的SDK的一种情况之一,可能会导致一些令人困惑的错误 . 作为REST API,它是无状态的,因此传递整个
user
属性集会告诉API您想要PATCH
所有这些值 .您还将另一个
user
对象传入me
对象(即您're replacing all of your property values with this other user'属性值):await graphClient.Me.Request().UpdateAsync(user);
相反,试试这个: