【问题标题】:c# how to get office 365 user photo using microsoft graph apic#如何使用microsoft graph api获取office 365用户照片
【发布时间】:2017-06-26 20:52:54
【问题描述】:

我希望能够在 Azure Active Directory 中获取所有用户的 office365 照片。

现在我可以使用 graph 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;
    }

但我不确定如何将https://graph.microsoft.io/en-us/docs/api-reference/v1.0/api/profilephoto_get 中的the getting photos part 集成到代码中。

感谢任何帮助或代码示例!

【问题讨论】:

    标签: c# microsoft-graph-api office365api outlook-restapi office365-restapi


    【解决方案1】:

    这有助于获取图像

     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 = "";
     }
    

    【讨论】:

      【解决方案2】:

      您使用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);
           }
       }
      

      【讨论】:

      • 如果我的应用已在 Azure AD 端点中注册,我是否仍能使用 Microsoft Graph API?因为现在我收到错误消息,说此 api 版本不支持我的应用程序
      • 是的。我使用在 Azure 门户中进行的应用注册测试了上面的代码,var photo = await graphClient.Me.Photo.Content.Request().GetAsync(); 行请求 v1.0 MS Graph API (graph.microsoft.com/v1.0/me/photo/$value)。
      • 当我尝试使用await graphClient.Users["userAddress@email.com"].Photo.Content.Request().GetAsync(); 访问用户的照片时,我收到另一个奇怪的错误消息Access Denied: Check Credentials and try again 但是我可以使用它来获取有关用户的任何其他信息,例如@987654327 @ 获取用户的邮箱
      【解决方案3】:

      2021 年你可以做到:

      Stream photo = await graphServiceClient.Me.Photo.Content.Request().GetAsync();
                  ViewData["Photo"] = Convert.ToBase64String((photo as MemoryStream).ToArray());
      

      然后在你的 html 中:

      <img src="data:image/png;base64, @ViewData["Photo"]" />
      

      【讨论】:

        【解决方案4】:

        如果你去图形资源管理器:https://developer.microsoft.com/en-us/graph/graph-explorer 您可以使用您的电子邮件地址登录。您应该会看到“我的照片”查询选项。有一个文档链接可以带你到这里: https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/profilephoto_get 您将看到一个表格,表明这仅适用于学校或工作帐户,不适用于您的个人 Microsoft 帐户。

        【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-07-08
        • 1970-01-01
        • 2015-10-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多