使用Microsoft Graph API,我可以获取Azure Active Directory租户中所有用户的列表,并确定他们是否有个人资料图片。然后,我想获取没有照片的用户列表并为他们上传照片,但是即使我使用的帐户对所有用户帐户都具有完全访问权限,并且该应用程序已设置为具有完全权限,API也会返回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.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配置文件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
对于您的问题,这是我编写的两种方法,对我有用:
我认为,您有一种获取有效访问 token 的方法。
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
塞布
关于c# - Microsoft Graph API更新其他用户的照片吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36503036/