Goodreads Developers discussion
Python example of how to use OAuth
date
newest »
newest »
Thank you for the example! The example is working perfectly for me.I'm trying to expand on this snippet but I'm having trouble getting the user's id. I changed the method from POST to GET and send an empty dictionary '{}' as the parameter, but I keep on getting a 401 when I try to do ANYTHING except adding a book to shelf. Any ideas why? (api/auth_user and updates/friends.xml for example, both respond with 401)
Thank you!
I figured it out. I was using the request token instead of the access token to make the API calls. *doh*. All seems to be working now. Thank you!
it's good news for me. It means that the problem on the server's side. Where is somebody from goodreads developer team?
I'm not from the developer team, but I'm trying to get this working, and I was getting a 401 trying get a request token until I changed the URL from http://www.goodreads.com/oauth/reques... to http://www.goodreads.com/oauth/reques... without the closing /. If I encounter any other problems, I'll try to update here.
I had a python script working based on the ruby example, but that didn't get me far enough to authenticate a user with Oauth. Steps that were already working broke when I tried to use the code shown above, so I started looking for differences.
I'm trying to develop a little app that will update my goodreads reading status based on another script that collects my kindle reading percentage. I'm trying to use this script by making a small modification but I can't get it to work. I get the following error.
Have you authorized me? (y/n) y
Traceback (most recent call last):
File "goodreads-oauth-example.py", line 35, in
raise Exception('Invalid response: %s' % response['status'])
Exception: Invalid response: 401
Can anyone assist me?
help us help you. Have you tried the suggestions above? did you get a different error message with and without those fixes?
Thanks for the response. I did try the suggestions above. I've gone through several versions of code trying to fix it. I also tried using Ruby and get the same 401 error. Here is the current complete python code.----------------------------
import oauth2 as oauth
import urlparse
request_token_url = 'http://www.goodreads.com/oauth/reques...'
authorize_url = 'http://www.goodreads.com/oauth/authorize'
access_token_url = 'http://www.goodreads.com/oauth/access...'
consumer_key = "jXXXXXXXXXXXXXXXXXg"
consumer_secret = "bXXXXXXXXXXXXXXXXXXXXXXXXU"
consumer = oauth.Consumer(consumer_key, consumer_secret)
client = oauth.Client(consumer)
resp, content = client.request(request_token_url, "POST")
if resp['status'] != '200':
raise Exception("Invalid response %s." % resp['status'])
request_token = dict(urlparse.parse_qsl(content))
authorize_link = '%s?oauth_token=%s' % (authorize_url,
request_token['oauth_token'])
print "Request Token:"
print " - oauth_token = %s" % request_token['oauth_token']
print " - oauth_token_secret = %s" % request_token['oauth_token_secret']
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'],
request_token['oauth_token_secret'])
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))
print "Access Token:"
print " - oauth_token = %s" % access_token['oauth_token']
print " - oauth_token_secret = %s" % access_token['oauth_token_secret']
print "You may now access protected resources using the access tokens above."
# this is the token you should save for future uses
token = oauth.Token(access_token['oauth_token'],
access_token['oauth_token_secret'])
# let's add a status
#
import urllib
client = oauth.Client(consumer, token)
# the book is: 1776
body = urllib.urlencode({'user_status[book_id]':1067, 'user_status[percent]':38})
headers = {'content-type': 'application/x-www-form-urlencoded'}
response, content = client.request('http://www.goodreads.com/user_status.xml',
'POST', body, headers)
# check that the new resource has been created
------------------------------------------
I am getting oath_token and oauth_token_secret returned. The 401 error is coming right after answering Y to have you authorized me.
Thanks for the help!
bump? I assume there is nothing wrong with the API itself and that the above referenced code is the issue, can someone confirm this?
here's a working python example. the oauth2 library doesn't seem to handle the oauth 1.0a protocol in the way we'd expect so i used rauth instead:http://www.goodreads.com/api/oauth_ex...
let me know if you still have issues.
I am having the same problem right now, and I found this post from 2013, Is there anyone still having the same trouble.
Slim,
Sorry for the late reply, but were you able to get the Jon's python Oauth example working? Were you able to get the session or did it fail on the add to shelf call?
I just tried it and it appears to be up-to-date. I was able to add two books to my to-read shelf for my test user.
Sorry for the late reply, but were you able to get the Jon's python Oauth example working? Were you able to get the session or did it fail on the add to shelf call?
I just tried it and it appears to be up-to-date. I was able to add two books to my to-read shelf for my test user.
I use the code provided by Kyle but keep getting this error. authorize_link = '%s?oauth_token=%s' % (authorize_url,request_token['oauth_token'])KeyError: 'oauth_token' . I am new to Python and Oauth, so pardon my ignorance. Please help. thanks
Siddharth wrote: "I use the code provided by Kyle but keep getting this error. authorize_link = '%s?oauth_token=%s' % (authorize_url,request_token['oauth_token'])KeyError: 'oauth_token' . I am new to Python and Oau..."
Consider using this example from official documentation. I can confirm it's working.
I am working with the API in Python, and have run into an odd error:Following the directions above using rauth, I have created a session with a stored access_token and access_token_secret. When I run a session.get for any OAuth materials (like owned books, which is my primary use for the API), I get a 401 error.
Here's the code I'm using:
from rauth.service import OAuth1Service, OAuth1Session
import xmltodict
BASE_URL = "http://www.goodreads.com/"
gr_service = OAuth1Service(
consumer_key = XXXXXXXXXXXXXX,
consumer_secret = XXXXXXXXXXXXXXXXXXXXX,
name = 'goodreads',
request_token_url = BASE_URL + 'oauth/request_token',
authorize_url = BASE_URL + 'oauth/authorize',
access_token_url = BASE_URL + 'oauth/access_token',
base_url = BASE_URL,
)
request_token, request_token_secret = gr_service.get_request_token(header_auth=True)
<-- snip the "yes to confirm" bit -->
temp_session = gr_service.get_auth_session(request_token, request_token_secret)
ACCESS_TOKEN = temp_session.access_token
ACCESS_TOKEN_SECRET = temp_session.access_token_secret
session = OAuth1Session(
consumer_key = XXXXXXXXXXXXXX,
consumer_secret = xxxxxxxxxxxxxxxxxxxxxxx,
access_token = ACCESS_TOKEN,
access_token_secret = ACCESS_TOKEN_SECRET,
)
get_user_url = "https://www.goodreads.com/api/auth_user"
resp = session.get(get_user_url)
user_dict = xmltodict(resp.content)['GoodreadsResponse']
get_owned_books_url = "https://www.goodreads.com/owned_books..." % user_dict['user']['@id']
book_resp = session.get(get_owned_books_url)
...at that point, if I check the book_resp, it's a 401 and throws an error if run through xmltodict.
Hi, Do you mean that when trying this API https://www.goodreads.com/api/index#o... you get a 401 error?
Is this the only endpoint that has this problem? Are you able to get this example working ?https://www.goodreads.com/api/oauth_e...
Thanks!
Yes, when attempting that API, I get an error. I get the error on most endpoints, and I'm using that example as a starting point.
Hi Feiyu,I managed to get access to my profile and I can add a book to my list. However, I am having issues pulling the entire list of my bookshelf (which is the reason I wanted the API in the first place).
The session code works
session = goodreads.get_auth_session(request_token, request_token_secret)
I tried accessing the list of my books
session.get('https://www.goodreads.com/shelf/list.xml')
The only output I get is:
How do I get the actual data?
Thank you!







The code lives at: http://gist.github.com/537923
The OAuth library used is: http://github.com/simplegeo/python-oa...
Please note that it is a complete script, not a step-by-step guide like the Ruby example provided in the API documentation. Nonetheless, I think it could be a nice starting point to write a Python example good enough to be included in the official documentation, and I hope it will be useful to some of you.