【问题标题】:Microsoft Graph API Update another user's photo?Microsoft Graph API 更新其他用户的照片?
【发布时间】:2019-01-16 07:36:07
【问题描述】:

使用 Microsoft Graph API,我能够获得 Azure Active Directory 租户中所有用户的列表,并确定他们是否有个人资料图片。然后我想获取没有照片的用户列表并为他们上传一张,但是即使我使用的帐户对所有用户帐户具有完全访问权限并且应用程序设置为具有完全权限,API 也会返回 403 错误到图形 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.ReadWrite Contacts.Read Contacts.ReadWrite Directory.AccessAsUser.All Directory.Read.All Directory.ReadWrite.All email 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.ReadWrite Mail.Send MailboxSettings.ReadWrite Notes.Create Notes.Read Notes.Read.All Notes.ReadWrite Notes.ReadWrite.All Notes.ReadWrite.CreatedByApp offline_access openid People.Read People.ReadWrite profile Sites.Read.All Tasks.Read Tasks.ReadWrite User.Read User.Read.All User.ReadBasic.All User.ReadWrite User.ReadWrite.All

【问题讨论】:

  • 您能否在jwt.calebb.net 输入您的令牌并给我们“scp”字段的值。这是您的令牌中的权限列表。谢谢。
  • 嗨 Venkat,我将 SCP 属性值添加到原始帖子中,因为它太长而无法作为评论输入。
  • 不确定该操作是否适用于 PATCH。它应该是一个 PUT。但是,我也重现了相同的问题(以管理员用户身份运行),这是跟踪。范围包含 user.readwrite.all 应该足以更新另一个用户的照片。 { "error": { "code": "ErrorAccessDenied", "message": "访问被拒绝。检查凭据并重试。", "innerError": { "request-id": "23559b24-d5e9-4dae-8311 -94f706320b4b", "日期": "2016-04-09T06:21:56" } } }
  • 如何获得令牌?来自 clientid + clientsecret ?
  • 嗨,Dan,我使用的是 PATCH,因为 Graph documentation 提到使用 PATCH 方法,但我也使用 PUT 进行了尝试,但它也不起作用。我开始认为 API 不允许更新其他用户的照片。

标签: c# azure office365 azure-active-directory microsoft-graph-api


【解决方案1】:

首先,您应该看看在 //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

Seb

【讨论】:

  • 感谢您让我了解 SDK,但我仍然收到相同的错误消息。我认为 API 不允许更新其他用户的照片,即使我使用的帐户应该可以访问它。
  • 所以你的范围或 OAUTH2 访问令牌有一个探针
  • 当我重新制作时,这是使用客户端库;)用户也是管理员。我不相信您可以通过应用程序+用户代码流来做到这一点,并且您需要一个仅限应用程序的令牌才能做到这一点。您是否尝试过客户端凭据流程?这对你有用吗?
【解决方案2】:

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

要更新组织中任何用户的照片,您的应用必须具有 User.ReadWrite.All 应用权限,并以自己的身份调用此 API,而不是代表用户。要了解详情,请参阅无需登录用户即可获得访问权限。

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

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

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

【讨论】:

  • 文档现在很清楚,但在我 3 年前提交问题时还没有。
  • 抱歉,帖子中没有看到日期
猜你喜欢
  • 2018-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多