首页 > 解决方案 > 尝试发布到 gfycat.com 时 Python 请求给出错误 ('Connection aborted.', error("(104, 'ECONNRESET')",))

问题描述

我正在执行代码:

import requests

clientID = redacted
secret =redacted

filename = "temp2.gif"
datakey = '{ "title":"random title"}'

r = requests.post("https://api.gfycat.com/v1/gfycats", data = datakey)
gfyname = r.json()["gfyname"]
print(r.json())
print(gfyname)

params_ = {'client_id': clientID, 'client_secret': secret, 'grant_type': 'client_credentials'}

r = requests.post('https://api.gfycat.com/v1/oauth/token', data=str(params_))

access_token = r.json()['access_token']

headers = {'Authorization': 'Bearer {}'.format(access_token)}
print(headers)
data = {'key': gfyname}
url = "https://filedrop.gfycat.com"
files = {gfyname: open(filename, 'rb')}

r = requests.post(url, data=data, files=files, headers=headers)
print(r)

在我收到错误消息的最后一个 request.post() 调用之前,这一切都执行得很好:

requests.exceptions.ConnectionError: ('Connection aborted.', error("(104, 'ECONNRESET')",))

我已经使用 gfycat api 文档验证了请求端点/参数。我检查了headers和的输出access_token。我已经阅读了请求文档,并且确实找到了一些我修复的东西。现在我被困在这里,试图找出连接中止的原因。

标签: pythonhttphttpspython-requestsgfycat

解决方案


使用pfycat

import pfycat
c = pfycat.Client("client_id", "client_secret")
r = c.upload('banana.gif')
print(r)

或者:看看pfycat 是怎么做的


推荐阅读