Zum Hauptinhalt springen
Chris’ wirre Gedankenwelt
  1. posts/

Python and Flattr API v2

In Fiddled around with python-oauth2 and flattr I described how to access the Flattr API. Since this post, the Flattr guys relased a new API which is currently still beta but already useful working. However, at least after a fix of the access token process ;-)

So. What exactly changed? Almost everything, starting with a completely new and different authentication and authorization process.

I think, with v2 it's easier to develop apps. The API seems to be more straight forward. The process to get an access token is much simpler. I also like that JSON is used by default. Also the documentation is better than before, and very easy to understand and follow. What I really like, I don't need python-oauth2 as dependency any more. Everything is working with e.g. httplib2 .

First we have to send the User to Flattr, for authorizing our app. Therefor we have to include our client_id, which is the KEYof our app on https://flattr.com/apps :

https://flattr.com/oauth/authorize?scope=thing&response_type=code& client_id=KEY&redirect_uri=http://localhost

After the authentication, we will be redirected to http://localhost/?code=NEW_CODE.Your app should be registered now at https://flattr.com/settings/applications .

The next step is already to get the access token, using HTTP Basic Auth:

>>> http = httplib2.Http(disable_ssl_certificate_validation=True)
>>> http.add_credentials(key, secret)
>>> code=NEW_CODE
>>> params = {'code': code,
... 'grant_type': 'authorization_code',
... 'redirect_uri': 'http://localhost/'}
>>> ret = http.request('https://flattr.com/oauth/token', 'POST', j.dumps(params), headers={'Content-Type': 'application/json'})
>>> ret
({'status': '200',
  'content-length': '73',
  'strict-transport-security': 'max-age=500',
  'set-cookie': 'PHPSESSID=3hceaj0umb533cipg462briq64; path=/; domain=.flattr.com; HttpOnly',
  'expires': 'Thu, 19 Nov 1981 08:52:00 GMT',
  'vary': 'Accept-Encoding',
  'server': 'lighttpd',
  'connection': 'close',
  '-content-encoding': 'gzip',
  'pragma': 'no-cache',
  'cache-control': 'no-store',
  'date': 'Sun, 20 Nov 2011 10:48:44 GMT',
  'content-type': 'application/json; charset=utf-8'},
'{"access_token":"NEW_ACCESS_TOKEN","token_type":"bearer"}')


That's it. We now have our access token and are able to use it.

Get my user information using the access token:

>>> headers = {'Content-Type': 'application/json', 'Authorization': 'Bearer NEW_ACCESS_TOKEN'}
>>> ret = http.request('https://api.flattr.com/rest/v2/user', headers=headers)
>>> ret
({'status': '200',
  'x-ratelimit-remaining': '4990',
  'content-location': 'https://api.flattr.com/rest/v2/user',
  '-content-encoding': 'gzip',
  'strict-transport-security': 'max-age=500',
  'vary': 'Accept-Encoding',
  'content-length': '274',
  'server': 'lighttpd',
  'connection': 'close',
  'x-ratelimit-limit': '5000',
  'date': 'Sun, 20 Nov 2011 10:54:01 GMT',
  'content-type': 'application/json'}, 
'{"type":"user","resource":"https:\\/\\/api.flattr.com\\/rest\\/v2\\/users\\/chrigl","link":"https:\\/\\/flattr.com\\/profile\\/chrigl","username":"chrigl","firstname":"Christoph","lastname":"Glaubitz","city":"","zip":"","province":"","cellphone":"","avatar":"","about":"","country":0}')
>>> simplejson.loads(ret[1])
{'about': '',
 'avatar': '',
 'cellphone': '',
 'city': '',
 'country': 0,
 'firstname': 'Christoph',
 'lastname': 'Glaubitz',
 'link': 'https://flattr.com/profile/chrigl',
 'province': '',
 'resource': 'https://api.flattr.com/rest/v2/users/chrigl',
 'type': 'user',
 'username': 'chrigl',
 'zip': ''}

And that's it. Thanks for reading, and don't forget to consult the Flattr API v2 documentation .