Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
A Python example of how to use OAuth on GoodReads
import oauth2 as oauth
import urlparse
url = ''
request_token_url = '%s/oauth/request_token/' % url
authorize_url = '%s/oauth/authorize/' % url
access_token_url = '%s/oauth/access_token/' % url
consumer = oauth.Consumer(key='Your-GoodReads-Key',
client = oauth.Client(consumer)
response, content = client.request(request_token_url, 'GET')
if response['status'] != '200':
raise Exception('Invalid response: %s' % response['status'])
request_token = dict(urlparse.parse_qsl(content))
authorize_link = '%s?oauth_token=%s' % (authorize_url,
print authorize_link
accepted = 'n'
while accepted.lower() == 'n':
# you need to access the authorize_link via a browser,
# and proceed to manually authorize the consumer
accepted = raw_input('Have you authorized me? (y/n) ')
token = oauth.Token(request_token['oauth_token'],
client = oauth.Client(consumer, token)
response, content = client.request(access_token_url, 'POST')
if response['status'] != '200':
raise Exception('Invalid response: %s' % response['status'])
access_token = dict(urlparse.parse_qsl(content))
# this is the token you should save for future uses
token = oauth.Token(access_token['oauth_token'],
# As an example, let's add a book to one of the user's shelves
import urllib
client = oauth.Client(consumer, token)
# the book is: "Generation A" by Douglas Coupland
body = urllib.urlencode({'name': 'to-read', 'book_id': 6801825})
headers = {'content-type': 'application/x-www-form-urlencoded'}
response, content = client.request('%s/shelf/add_to_shelf.xml' % url,
'POST', body, headers)
# check that the new resource has been created
if response['status'] != '201':
raise Exception('Cannot create resource: %s' % response['status'])
print 'Book added!'

This comment has been minimized.

Copy link

@happymishra happymishra commented Jan 16, 2015

It is giving error invalid request 401 .please help


This comment has been minimized.

Copy link

@aliceinwire aliceinwire commented May 4, 2015

@happymishra try to use this script instead and doing first the

usually that error is because you are not logged in or authorized. you are probably mistaking something.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment