python - 使用 Python 通过 GroupMe 的图像服务路由图像
问题描述
我正在使用requests
向 GroupMe 的图像服务发出 POST 请求,该请求应返回托管图像的 URL,我可以使用该 URL 将其发布到 GroupMe 线程。文档提到我需要我的访问令牌和有效负载中的二进制图像数据才能执行此操作。
这是一个非常简单的示例,说明了我的代码当前如何执行此操作:
import requests
access_token = 'my_access_token'
img_path = 'picture_name.jpg'
img_service_url = 'https://image.groupme.com/pictures'
r = requests.post(img_service_url, files={'file': img_path})
编辑:
我查看了groupy.api.endpoint
模块的文档和源代码Groupy
(https://groupy.readthedocs.io/en/v0.6.2/_modules/groupy/api/endpoint.html#Images)并更新了我的脚本(如上所示)以使用相同requests
的函数参数,但无济于事。现在代码返回一个500
.
解决方案
This worked for me (avatar.jpeg is in the same folder as my testing.py code below)
# curl 'https://image.groupme.com/pictures'
# -X POST
# -H "X-Access-Token: $GM_TOKEN"
# -H "Content-Type: image/jpeg"
# --data-binary @AwesomePicture.jpg
import requests
data = open('./avatar.jpeg', 'rb').read()
res = requests.post(url='https://image.groupme.com/pictures',
data=data,
headers={'Content-Type': 'image/jpeg',
'X-Access-Token': 'ACCESS_TOKEN'})
print(res.content)
OUTPUT
b'{"payload":{"url":"https://i.groupme.com/100x100.jpeg.5b71f15633f6454ca6a3a6b3e267a3fb","picture_url":"https://i.groupme.com/100x100.jpeg.5b71f15633f6454ca6a3a6b3e267a3fb"}}\n'
推荐阅读
- html - 绝对左下角的引导按钮位置
- php - Wordpress 主题 functions.php 不用于非登录用户
- javascript - 我的内容脚本如何从 HTML Web 可访问资源页面访问元素?
- xamarin.forms - 如何为 Xamarin 按钮编写 UITest?
- postgresql - pgAdmin 功能下拉不扩展
- python - 如何将我的文件保存到我刚刚创建的目录中?
- c++ - 下面的变量是如何分配的?
- java - 资源未从 web.xml 注入数据源 - Java 框架 - Payara Micro - NullPointerException
- powershell - Azure DevOps & 在尝试创建新分支时无法确定工作区
- c# - 初始化 ServiceClient 请参阅错误“参数 1:无法从 'System.Net.Http.HttpClient' 转换为 'System.Net.Http.DelegatingHandler'”