首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >请求发布到imgur

请求发布到imgur
EN

Stack Overflow用户
提问于 2012-12-05 23:52:09
回答 1查看 818关注 0票数 1
代码语言:javascript
复制
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的相册,而返回的字符串是

代码语言:javascript
复制
{
    "albums": {
        "id": "IMGURID",
        "title": "",
        "description": "",
        "privacy": "public",
        "cover": "",
        "order": 0,
        "layout": "blog",
        "datetime": "2012-12-05 15:48:21",
        "link": "IMGUR LINK",
        "anonymous_link": "ANONYLINK"
    }
}

有没有人有使用请求设置专辑标题的解决方案?

这里有一个指向imgur API文档http://api.imgur.com/resources_auth的链接

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-12-05 23:54:14

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

使用json模块进行编码:

代码语言:javascript
复制
import json

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

您可以在使用http://httpbin/post POST echo service时看到这一点

代码语言:javascript
复制
>>> import json, requests, pprint
>>> headers = {'content-type': 'application/json'}
>>> payload = {"title":"album title"}
>>> pprint.pprint(requests.post('http://httpbin.org/post', payload, headers=headers).json)
{u'args': {},
 u'data': u'title=album+title',
 u'files': {},
 u'form': {},
 u'headers': {u'Accept': u'*/*',
              u'Accept-Encoding': u'gzip, deflate, compress',
              u'Connection': u'keep-alive',
              u'Content-Length': u'17',
              u'Content-Type': u'application/json',
              u'Host': u'httpbin.org',
              u'User-Agent': u'python-requests/0.14.2 CPython/2.7.3 Darwin/11.4.2'},
 u'json': None,
 u'origin': u'xx.xx.xx.xx',
 u'url': u'http://httpbin.org/post'}
>>> pprint.pprint(requests.post('http://httpbin.org/post', json.dumps(payload), headers=headers).json)
{u'args': {},
 u'data': u'{"title": "album title"}',
 u'files': {},
 u'form': {},
 u'headers': {u'Accept': u'*/*',
              u'Accept-Encoding': u'gzip, deflate, compress',
              u'Connection': u'keep-alive',
              u'Content-Length': u'24',
              u'Content-Type': u'application/json',
              u'Host': u'httpbin.org',
              u'User-Agent': u'python-requests/0.14.2 CPython/2.7.3 Darwin/11.4.2'},
 u'json': {u'title': u'album title'},
 u'origin': u'xx.xx.xx.xx',
 u'url': u'http://httpbin.org/post'}

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

代码语言:javascript
复制
r = client.post("http://api.imgur.com/2/account/albums.json", json=payload)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13727132

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档