c#如何使用microsoft graph api获取office 365用户照片
问题描述:
我希望能够在Azure Active Directory中获取所有用户的office365照片。c#如何使用microsoft graph api获取office 365用户照片
现在我可以在使用图形SDK
GraphServiceClient graphClient = SDKHelper.GetAuthenticatedClient();
public async Task<string> GetMyEmailAddress(GraphServiceClient graphClient)
{
User me = await graphClient.Me.Request().Select("mail,userPrincipalName").GetAsync();
return me.Mail ?? me.UserPrincipalName;
}
来获取当前用户的电子邮件,但我不知道如何将the getting photos part从https://graph.microsoft.io/en-us/docs/api-reference/v1.0/api/profilephoto_get融入代码。
任何帮助或代码示例表示赞赏!
答
你得到使用graphClient.Me.Photo.Content
将取回照片的二进制数据流中的照片:
public async Task GetPictureAsync()
{
GraphServiceClient graphClient = GetGraphServiceClient();
var photo = await graphClient.Me.Photo.Content.Request().GetAsync();
using (var fileStream = File.Create("C:\\temp\\photo.jpg"))
{
photo.Seek(0, SeekOrigin.Begin);
photo.CopyTo(fileStream);
}
}
将我仍然能够使用微软图形API,如果我的应用程序在Azure的AD注册终点?因为现在我得到错误,说我的应用程序不支持此api版本 – yfan183
是的。我测试了上面的代码,我在Azure Portal中做了一个应用程序注册,这一行'var photo = await graphClient.Me.Photo.Content.Request()。GetAsync();''请求v1.0 MS Graph API(https://graph.microsoft.com/v1.0/me/photo/$value)。 – RasmusW
当我尝试使用'await graphClient.Users [“[email protected]”]访问用户的照片时,出现另一个奇怪的错误,说'访问被拒绝:检查凭证并再试一次'。Photo.Content.Request ().GetAsync();'然而,我可以使用它来获取关于用户的其他信息,例如'await graphClient.Users [“[email protected]”]。Request()。Select(“mail”)。GetAsync ();'获取用户的电子邮件 – yfan183