请求邮寄至imgu

2024-04-26 14:15:22 发布

您现在位置:Python中文网/ 问答频道 /正文

from oauth_hook import OAuthHook
import requests, json
OAuthHook.consumer_key = "KEYHERE" 
OAuthHook.consumer_secret = "SECRET HERE"
oauth_hook = OAuthHook("TOKEN_KEY_HERE", "TOKEN_SECRET_HERE", header_auth=True)
headers = {'content-type': 'application/json'}
client = requests.session(hooks={'pre_request': oauth_hook}, headers=headers)
payload = {"title":"album title"}
r = client.post("http://api.imgur.com/2/account/albums.json",payload)
print r.text

这应该创建一个相册,标题是album title,而不是返回字符串

^{pr2}$

有没有人有一个解决方案来设置专辑标题使用请求?在

以下是指向imgur API文档的链接http://api.imgur.com/resources_auth


Tags: importclienttokenauthjsonsecrethereconsumer
1条回答
网友
1楼 · 发布于 2024-04-26 14:15:22

您没有发布JSON数据;相反,它将被转换为URL编码的数据。requests不提供自动JSON编码,即使将内容类型设置为application/json。在

使用json模块编码:

import json

r = client.post("http://api.imgur.com/2/account/albums.json", json.dumps(payload))

使用^{} POST echo service时可以看到:

^{pr2}$

如果您使用的是requests2.4.2或更高版本,则可以将编码留给库;只需将有效负载作为json关键字参数传入;在这种情况下,它还将设置正确的Content-Type头:

r = client.post("http://api.imgur.com/2/account/albums.json", json=payload)

相关问题 更多 >