使用 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 值为:
日历.读取日历.ReadWrite 联系人.读取联系人.ReadWrite 目录.AccessAsUser.所有目录.读取.所有目录.ReadWrite.所有电子邮件 Exchange.管理文件.读取文件.读取.选定的文件.读写文件.ReadWrite.AppFolder 文件.ReadWrite .选定的 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离线访问openid People.Read People.ReadWrite 配置文件 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/microsoftgraph
这是我使用它创建的完整示例:https://github.com/Mimetis/NextMeetingsForGraphSample 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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)