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
This should create an album with the title album title how instead the return string is
{
"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"
}
}
Does anyone have a solution for setting the album title using requests?
here is a link to the imgur API docs http://api.imgur.com/resources_auth
You are not posting JSON data; instead it’ll be converted to URL-encoded data instead.
requestsdoes not provide automatic JSON encoding, even if you do set the content type toapplication/json.Use the
jsonmodule to encode:You can see this when using the
http://httpbin/postPOST echo service:If you are using
requestsversion 2.4.2 or newer, you can leave the encoding to the library; simply pass in the payload as thejsonkeyword argument; incidentally it’ll also set the correct Content-Type header in that case: