Microsoft Graph API 更新其他用户的照片?

Microsoft Graph API Update another user's photo?

使用 Microsoft Graph API 我能够获得 Azure Active Directory 租户中所有用户的列表,并确定他们是否有个人资料照片。然后我想获取没有照片的用户列表并为他们上传一张照片,但是 API returns 出现 403 错误,即使我使用的帐户具有对所有用户帐户的完全访问权限并且该应用程序设置为具有对 Graph API 的完全权限。

using (HttpClient client = new HttpClient())
{
    client.BaseAddress = new Uri("https://graph.microsoft.com/");
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("image/jpeg"));
    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", oauthToken);

    // HTTP GET                
    HttpResponseMessage response = await client.PatchAsync($"v1.0/users/{emailAddress}/photo/$value", byteContent);
    if (!response.IsSuccessStatusCode)
    {
        throw new Exception("Error!");
    }
}

403 禁止

是否无法使用 Graph API 执行此操作,还是我在某处缺少权限?

SCP值为: Calendars.Read Calendars.Read写 Contacts.Read Contacts.Read写 Directory.AccessAsUser.All Directory.Read.All Directory.ReadWrite.All 电子邮件 Exchange.Manage Files.Read Files.Read.Selected Files.ReadWrite Files.ReadWrite.AppFolder Files.ReadWrite.Selected full_access_as_user Group.Read.All Group.ReadWrite.All Mail.Read Mail.Read写入Mail.SendMailboxSettings.ReadWriteNotes.CreateNotes.ReadNotes.Read.全部Notes.Read写入Notes.Read写入.全部Notes.Read写入.CreatedByApp offline_access openid People.Read People.Read写入配置文件Sites.Read.All Tasks.Read Tasks.Read写入User.Read User.Read.All User.Read基本.全部User.Read写入User.Read写入.全部

首先,您应该看一下在 //Build 2016 期间发布的新 Microsoft Graph SDK 这是 Microsoft Graph SDK 的 Github:https://github.com/microsoftgraph
这是我使用它创建的完整示例: https://github.com/Mimetis/NextMeetingsForGraphSample

对于你的问题,这里有我写的两种对我有用的方法: 我假设,您有一种获取有效访问令牌的方法。

using (HttpClient client = new HttpClient())
{
    var authResult = await AuthenticationHelper.Current.GetAccessTokenAsync();
    if (authResult.Status != AuthenticationStatus.Success)
        return;

    client.DefaultRequestHeaders.Add("Authorization", "Bearer " + authResult.AccessToken);

    Uri userPhotoEndpoint = new Uri(AuthenticationHelper.GraphEndpointId + "users/" + userIdentifier + "/Photo/$value");
    StreamContent content = new StreamContent(image);
    content.Headers.Add("Content-Type", "application/octet-stream");

    using (HttpResponseMessage response = await client.PutAsync(userPhotoEndpoint, content))
    {
        response.EnsureSuccessStatusCode();
    }
}

如果你使用 Microsoft Graph SDK,那将非常简单:)

GraphServiceClient graphService = new GraphServiceClient(AuthenticationHelper.Current);
var photoStream = await graphService.Users[userIdentifier].Photo.Content.Request().PutAsync(image); //users/{1}/photo/$value

塞布

documentation 非常清楚更新其他用户的照片。

To update the photo of any user in the organization, your app must have the User.ReadWrite.All application permission and call this API under its own identity, not on behalf of a user. To learn more, see get access without a signed-in user.

这意味着您需要应用程序的访问令牌,而不是用户访问您的应用程序时获得的令牌。此 page 展示了如何获取应用程序访问令牌。这也意味着您必须向您的应用程序授予应用程序权限,而这常常被遗忘。

获得令牌后,您可以在 https://jwt.ms 上查看它以查看令牌中声明的说明。

您是在谈论令牌中的 scp 声明。仅当您拥有用户令牌而不是应用程序令牌时才存在。