How to get user image in Microsoft Graph using python

最后都变了- 提交于 2020-07-10 08:56:48

问题


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:


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)


来源:https://stackoverflow.com/questions/58745561/how-to-get-user-image-in-microsoft-graph-using-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!