c#如何使用microsoft graph api获取office 365用户照片

yfa*_*183 7 c# outlook-restapi office365api office365-restapi microsoft-graph-api

我希望能够在 Azure 活动目录中获取所有用户的 office365 照片。

现在我可以使用图形 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;
    }
Run Code Online (Sandbox Code Playgroud)

但我不知道如何将集成在得到照片部分来自https://graph.microsoft.io/en-us/docs/api-reference/v1.0/api/profilephoto_get到代码。

任何帮助或代码示例表示赞赏!

小智 6

这有助于获取图像

 GraphServiceClient graphServiceClient = GetAuthenticatedGraphServiceClient();

 Stream photo = await graphServiceClient.Me.Photo.Content.Request().GetAsync();

 if (photo != null)
 {
     MemoryStream ms = new MemoryStream();
     photo.CopyTo(ms);
     byte[] buffer = ms.ToArray();
     string result = Convert.ToBase64String(buffer);
     string imgDataURL = string.Format("data:image/png;base64,{0}", result);
     ViewBag.ImageData = imgDataURL;
 }
 else
 {
      ViewBag.ImageData = "";
 }
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明


Ras*_*usW 1

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);
     }
 }
Run Code Online (Sandbox Code Playgroud)

  • 当我尝试使用“await graphClient.Users["userAddress@email.com"].Photo.Content.Request( ).GetAsync();` 但是,我可以使用它来获取有关用户的任何其他信息,例如 `await graphClient.Users["userAddress@email.com"].Request().Select("mail").GetAsync( );` 获取用户的电子邮件 (2认同)