0
votes

I tried using GET https://graph.microsoft.com/v1.0/me/photo/$value to get the user's image/photo but it only returns an HTTP 200 status code. How can I get the binary data?

graph_url = 'https://graph.microsoft.com/v1.0'

def get_user(token):
  graph_client = OAuth2Session(token=token)
  # Send GET to /me
  user = graph_client.get('{0}/me'.format(graph_url))
  # Return the JSON result*
  return user.json()


def get_image(token):
  graph_client = OAuth2Session(token=token)
  # Send GET to /me
  image = graph_client.get('{0}/me/photo/$value'.format(graph_url))

  print('image_graph',image)
  return image

I'm expecting to receive a binary data

1
What have you checked to say the response doesn't contain the image? 200 is ok so I would say it should contain it. - luis.parravicini
I tried to print on the console and this is the result "image_graph <Response [200]>" - Leonard Viva
That's only the string representation of the returned object (Response). I don't know the library you are using but check the documentation to see how to get the data from the Response object. - luis.parravicini
What are you expecting image to be here? $value is going to return the RAW binary content, it isn't an encoded value or URL. - Marc LaFleur

1 Answers

1
votes

Since OAuth2Session.get method returns Response object, you could access response body as bytes via Content property.

The following example demonstrates how to download a profile photo and save to local file:

graph_client = OAuth2Session(token=token)
resp = graph_client.get('{0}/me/photo/$value'.format(graph_url))
if resp.status_code == 200:
    with open(path, 'wb') as f:
        f.write(resp.content)