1

I'm using an older implementation of an httpClient call to the Microsoft Graph to get a user's photo. The following code works but I'm using the Graph Client SDK now and things are done a little differently. I'm having a difficult time converting the code over as the samples and other references online don't seem to have the same methods.

Old code:

var response = await httpClient.GetAsync($"{webOptions.GraphApiUrl}/beta/me/photo/$value");
byte[] photo = await response.Content.ReadAsByteArrayAsync();
return Convert.ToBase64String(photo);

New code:

var graphServiceClient = await graphClient.GetAuthenticatedGraphClientAsync(HttpContext);
Stream photo = await graphServiceClient.Me.Photo.Content.Request().GetAsync();

I've tried examples from here and here but I'm a bit lost since ReadAsByteArrayAsync() is not available on the new photo object.

Jason Shave
  • 2,462
  • 2
  • 29
  • 47
  • Take a look at the following post: https://techcommunity.microsoft.com/t5/Microsoft-Graph/How-to-get-user-photo-in-C-using-Graph-API/td-p/94781 -- the OP in the post, provides the answer to his own question, and I believe you will find the code in that answer for the use case you are looking for. The answer has an extension method that makes a call to GraphAPI to get an image via `ReadAsStreamAsync()` but returns a `Task` to callers. – David Tansey Apr 29 '19 at 03:16
  • Thanks but that is the same method I was using. The return object is different in the new method. It is a ‘stream’ type. – Jason Shave Apr 29 '19 at 03:22
  • The OPs question use the same method but the _answer is different_ and deals directly with `stream` -- look closely at the code. – David Tansey Apr 29 '19 at 03:25

1 Answers1

4

Since msgraph-sdk-dotnet for Get photo endpoint supports to return photo content as a Stream only

public interface IProfilePhotoContentRequest : IBaseRequest
{
    /// <summary>Gets the stream.</summary>
    /// <returns>The stream.</returns>
    Task<Stream> GetAsync();

    /// <summary>Gets the stream.</summary>
    /// <param name="cancellationToken">The <see cref="T:System.Threading.CancellationToken" /> for the request.</param>
    /// <param name="completionOption">The <see cref="T:System.Net.Http.HttpCompletionOption" /> to pass to the <see cref="T:Microsoft.Graph.IHttpProvider" /> on send.</param>
    /// <returns>The stream.</returns>
    Task<Stream> GetAsync(
      CancellationToken cancellationToken,
      HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead);

    //...
}

and Convert.ToBase64String Method accepts a byte array, the following example demonstrates how to convert the original example:

var graphServiceClient = await graphClient.GetAuthenticatedGraphClientAsync(HttpContext);
var photoStream = await graphServiceClient.Me.Photo.Content.Request().GetAsync();
var photoBytes = ReadAsBytes(photoStream);
var result = Convert.ToBase64String(photoBytes);

where

//Convert Stream to bytes array  
public static byte[] ReadAsBytes(Stream input)
{
    using (var ms = new MemoryStream())
    {
        input.CopyTo(ms);
        return ms.ToArray();
    }
}
Vadim Gremyachev
  • 57,952
  • 20
  • 129
  • 193