繁体   English   中英

Microsoft Graph API更新另一个用户的照片?

[英]Microsoft Graph API Update another user's photo?

使用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 FORBIDDEN

是不是可以使用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

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

勒布

关于更新其他用户的照片, 文档非常清楚。

要更新组织中任何用户的照片,您的应用必须具有User.ReadWrite.All应用程序权限,并以其自己的身份调用此API,而不是代表用户。 要了解更多信息,请参阅没有登录用户的访问权限。

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

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

你在谈论令牌中的scp声明。 只有在您拥有用户令牌而非应用程序令牌时才会出现这种情况。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM