如何使用 python 在 Microsoft Graph 中显示用户的图像

我尝试使用 GET https://graph.microsoft.com/v1.0/me/photo/$value来获取用户的图像/照片,但它只返回 HTTP 200 状态代码。如何获取二进制数据?

我也尝试过使用content.property ,如在类似帖子中建议的那样,但 get a.format不是字典的属性。

@app.route("/photo")

def get_photo():

    token = _get_token_from_cache(app_config.SCOPE)

    if not token:

        return redirect(url_for("login"))

    photo = requests.get(app_config.PHOTO_ENDPOINT,  

            headers={'Authorization': 'Bearer ' + token['access_token']})

    print(photo.status_code)

    return photo


守候你守候我
浏览 103回答 3
3回答

慕哥9229398

获取个人资料照片,并可选择保存本地副本。返回原始照片数据、HTTP 状态代码、内容类型和保存的文件名的元组。请参阅此示例。def profile_photo(session, *, user_id='me', save_as=None):    """Get profile photo, and optionally save a local copy.    session = requests.Session() instance with Graph access token    user_id = Graph id value for the user, or 'me' (default) for current user    save_as = optional filename to save the photo locally. Should not include an              extension - the extension is determined by photo's content type.    Returns a tuple of the photo (raw data), HTTP status code, content type, saved filename.    """    endpoint = 'me/photo/$value' if user_id == 'me' else f'users/{user_id}/$value'    photo_response = session.get(api_endpoint(endpoint),                                 stream=True)    photo_status_code = photo_response.status_code    if photo_response.ok:        photo = photo_response.raw.read()        # note we remove /$value from endpoint to get metadata endpoint        metadata_response = session.get(api_endpoint(endpoint[:-7]))        content_type = metadata_response.json().get('@odata.mediaContentType', '')    else:        photo = ''        content_type = ''    if photo and save_as:        extension = content_type.split('/')[1]        filename = save_as + '.' + extension        with open(filename, 'wb') as fhandle:            fhandle.write(photo)    else:        filename = ''    return (photo, photo_status_code, content_type, filename)

30秒到达战场

如果您想在网页上显示生成的图像,则基于原始问题的代码的替代方法。from base64 import b64encode@app.route("/photo")def get_photo():&nbsp; &nbsp; token = _get_token_from_cache(app_config.SCOPE)&nbsp; &nbsp; if not token:&nbsp; &nbsp; &nbsp; &nbsp; return redirect(url_for("login"))&nbsp; &nbsp; response = requests.get(&nbsp; &nbsp; &nbsp; &nbsp; app_config.PHOTO_ENDPOINT,&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; headers={'Authorization': 'Bearer ' + token['access_token']}&nbsp; &nbsp; )&nbsp; &nbsp; content_type = response.raw.getheader('Content-Type')&nbsp; &nbsp; return render_template('index.html',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;photo_data=b64encode(response.content),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;photo_content_type=content_type)然后在index.html模板中,您可以像这样显示照片:<html>&nbsp; <body>&nbsp; &nbsp; <img src="data:{{ photo_content_type }};base64,{{ photo_data }}" />&nbsp; </body></html>

梦里花落0921

Call Api: -&nbsp;&nbsp; &nbsp; Axios.get('https://graph.microsoft.com/v1.0/me/photo/$value', {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; headers: { 'Authorization': 'Bearer '+AccessToken },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; responseType: 'blob'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }).then(o => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const url = window.URL || window.webkitURL;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const blobUrl = url.createObjectURL(o.data);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.setState({ imageUrl: blobUrl });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })JSX: -&nbsp;<img alt="image" src={this.state.imageUrl} />
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python