问题
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