如何使用python在Microsoft Graph中获取用户图像 [英] How to get user image in Microsoft Graph using python

查看:43
本文介绍了如何使用python在Microsoft Graph中获取用户图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 GET https://graph.microsoft.com/v1.0/me/photo/$value 来获取用户的图片/照片,但它只返回一个 HTTP200 状态码.我怎样才能得到二进制数据?

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

推荐答案

由于 OAuth2Session.get 方法返回 Response 对象,您可以通过 Content 属性.

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)

这篇关于如何使用python在Microsoft Graph中获取用户图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆