2

I am trying to retrieve the picture of a user within bot code using the following url using a Bearer token

https://mytenant.sharepoint.com/_layouts/15/userphoto.aspx?size=S&username=john.doe@mytenant.com

When I check via a browser the picture shows up fine. When trying to retrieve it programmatically, it returns an image but its the default placeholder silhouette grey image.

enter image description here

I gave the app in AAD a good amount of permissions

enter image description here

Any idea what I am missing? Tried all options I could think of.

My code is as follows:

using (HttpClient httpClient = new HttpClient())
{
    byte[] imageBytes = null;
    httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", bearer);
    var responseMessage = await httpClient.GetAsync(@"https://mytenant.sharepoint.com/_layouts/15/userphoto.aspx?size=S&username=john.doe@mytenant.com");
    imageBytes = responseMessage.Content.ReadAsByteArrayAsync().Result;
    string filePath = "C:\\Data\\Trash\\MyImage.jpg";
    File.WriteAllBytes(filePath, imageBytes);
}
JakeUT
  • 359
  • 1
  • 4
  • 16

1 Answers1

0

You are doing nothing wrong, just the page userphoto.aspx does not understand bearer token authentication. It understands only cookies, as far as I know.

Since you have User.ReadBasic.All anyway you can just use graph to get the photo instead. Replace

https://mytenant.sharepoint.com/_layouts/15/userphoto.aspx?size=S&username=john.doe@mytenant.com

with:

https://graph.microsoft.com/v1.0/users/john.doe@mytenant.com/photo/$value

And it should go

Nikolay
  • 10,752
  • 2
  • 23
  • 51