1

我尝试使用 Microsoft Graph API 从 O365 获取用户个人资料图片。当我使用以下 API 时,它只返回与个人资料图片相关的元数据。

https://graph.microsoft.com/beta/me/photo

通过https://graph.microsoft.com/beta/me/photo/ $value 返回一个没有任何意义的乱码对象。但是,我认为这是与用户个人资料相关的数据。需要帮助将这些数据提取到 base64 中。

4

2 回答 2

2

为了使该照片在视图中可见,我们必须将响应转换为 64 字节。我已经通过下面的代码按项目完成了这项工作。希望这个答案对某人有用..

 HttpResponseMessage response1 =  await httpClient.GetAsync("https://graph.microsoft.com/v1.0/me/photos/96x96/$value");
 using (Stream responseStream = await response1.Content.ReadAsStreamAsync())
{
using (FileStream fs = new FileStream(@"D:\image.jpg", FileMode.Create))
 {
     MemoryStream ms = new MemoryStream();
    responseStream.CopyTo(ms);
    byte[] buffer = ms.ToArray();
   string result = Convert.ToBase64String(buffer);
    HttpContext.Session[AppConstants.UserImage] = String.Format("data:image/gif;base64,{0}", result);
    responseStream.Close();
    }
}
于 2019-10-11T10:04:32.367 回答
2

返回的数据是图像类型的二进制数据。如果您使用 JavaScript 检索用户照片,请在 XMLHttpRequest 中以 blob 类型获取照片数据,然后从响应中检索 blob URL。供你参考:

 var request = new XMLHttpRequest;
 var photoUri=config.endpoints.graphApiUri + "/v1.0/me/photo/$value";
 request.open("GET",photoUri);
 request.setRequestHeader("Authorization","Bearer "+token);
 request.responseType = "blob";
 request.onload = function (){

 if(request.readyState == 4 && request.status == 200){

 var image = document.createElement("img");
 var url = window.URL || window.webkitURL;
 var blobUrl = url.createObjectURL(request.response);
 image.src = blobUrl;
  document.getElementById("UserShow").appendChild(image);
 }

 };
 request.send(null); 
于 2016-01-12T06:33:31.020 回答