Reputation: 152
I have tried to get user profile pic from O365 using Microsoft Graph API. When I used following API it returns only the metadata related to the profile pic.
https://graph.microsoft.com/beta/me/photo
Through https://graph.microsoft.com/beta/me/photo/$value returns a gibberish object which doesn't make any sense. However, I believe that it is the data related to the user profile. Need help to extract those data into base64.
Upvotes: 1
Views: 3352
Reputation: 178
For making that photo viewable in view we have to convert the response in 64 byte. I have make this done in by project by below code. Hope this answer useful for someone..
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();
}
}
Upvotes: 2
Reputation: 56
The returned data is the binary data of the image type. If you use JavaScript to retrieve the user photo, please get the photo data as blob type in a XMLHttpRequest, and then retrieve the blob URL from the response. For your reference:
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);
Upvotes: 2