首页 > 解决方案 > 使用 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模块的文档和源代码Groupyhttps://groupy.readthedocs.io/en/v0.6.2/_modules/groupy/api/endpoint.html#Images)并更新了我的脚本(如上所示)以使用相同requests的函数参数,但无济于事。现在代码返回一个500.

标签: pythonpython-3.xpython-requestsgroupme

解决方案


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'

推荐阅读