【发布时间】:2021-03-09 23:11:19
【问题描述】:
我想使用 python 检索我的个人资料图片并将其显示在profiles.html上。
"...gif;base64,(变量)" alt="profile">?
我认为我需要更改(可变)响应,但是我在转换它以及我应该将它转换为什么方面遇到了困难。
这是我尝试过的:
profiles.html:
{% extends "tutorial/layout.html" %}
{% block content %}
<h1>Profile</h1>
<img id="test" src="data:image/gif;base64,{{output_image}}" alt="profile">
{% endblock %}
graph_helper.py
def get_profiles(token):
graph_client = OAuth2Session(token=token)
profiles = graph_client.get(f"{graph_url}/me/photo/$value", stream=True)
return profiles.raw.read()
views.py
def profiles(request):
token = get_token(request)
profiles = get_profiles(token)
return render(request, 'tutorial/profiles.html', {"output_image": profiles})
但是我收到错误代码
无法访问此站点数据:image/gif;base64,b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x01\x00
\x00\x00 \x00\xff\xdb\x00C\x00\x08\x06\x06\x07\x06\x05\x08\x07\x07\x07\t\t\x08\n\x0c\x14\r\x0c\x0b\x0b \x0c\x19\x12\x13\x0f\x14\x1d\x1a\x1f\x1e\x1d\x1a\x1c\x1c $.' ",#\x1c\x1c(7),01444\x1f'9=82<.342 x00 x08 x97 xc5 err_invalid_url>
【问题讨论】:
-
您可以在 Microsoft Graph Explorer 中尝试上述 Graph API 调用,看看是否可以重现该问题?
-
@Dev 感谢您的回复,它让我重新思考我应该如何提出这个问题,我将改变它以反映问题。我将如何将收到的代码转换为 .html 显示我的个人资料图片所需的正确代码? "...gif;base64,(插入代码)" alt="profile">?
-
当您使用 /photo/$value 端点获取个人资料照片的二进制数据时,您需要将数据转换为 base-64 字符串才能将其添加为电子邮件附件。我看到您想在网页上显示图像,从图像创建一个内存对象,并使该对象成为图像元素的源。我记得一个 JavaScript 示例: const url = window.URL ||窗口.webkitURL;常量 blobUrl = url.createObjectURL(image.data); document.getElementById(imageElement).setAttribute("src", blobUrl);
-
Graph 会将数据作为流返回给您,您需要将该流转换为 base64
-
以上内容对您的进步有帮助吗?
标签: python django microsoft-graph-api