Goodreads Developers discussion
Python example of how to use OAuth
date
newest »


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'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?


----------------------------
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!


http://www.goodreads.com/api/oauth_ex...
let me know if you still have issues.

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.

KeyError: 'oauth_token' . I am new to Python and Oauth, so pardon my ignorance. Please help. thanks

KeyError: 'oauth_token' . I am new to Python and Oau..."
Consider using this example from official documentation. I can confirm it's working.

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.

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!


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.