Goodreads Developers discussion
questions
>
Not Authorised error while obtaining owned books by friends
date
newest »


Sorry to hear you're having trouble. I didn't see anything immediately wrong with your code, but it's been quite some time since I've done Python development.
Are you able to access data if you hardcode the url params in the url, like:
session.get('https://www.goodreads.com/owned_books...') ?
Have you tried using a tool like Postman (https://chrome.google.com/webstore/de...)? It allows you to test out oauth api endpoints by providing your api keys and oauth tokens. I tried the owned books endpoint myself through it and was able to access it without any issues. Maybe try that and see if you're able to hit the endpoint?
Sorry I can't be more help.

Thanks for your response as it made me confident that the API is working correctly and I need to even spend more time in figuring out what's wrong with the code.
I could make it work eventually.
The problem was with the url.
As mentioned in the API documentation, I have been using the following URL:
https://www.goodreads.com/owned_books...
And this was not working.
I changed the URL to the following:
https://www.goodreads.com/owned_books...
and everything is working fine. Probably something that can be mentioned in the documentation?
I got the cue from checking the URL I have been using for obtaining friends of a user(as mentioned in the documentation):
https://www.goodreads.com/friend/user...
When I removed the format:xml parameter from my POSTMAN request I was getting a correct HTML response and hence I think that there is something wrong with the API. Could you please ask the engineering team to have a look?

session.get("https://www.goodreads.com/owned_books...", params={'id': user})
I am trying to write a python program that can list all owned books by my friends. However, I am getting a status 401 as a response for my call to get the list of books owned by a user. It seemed to be working fine to find the list of friends for a user, but I encounter the authorisation issue for list of owned books. Following is the code I have been using, could any one point to the mistake I am making?
import os
import time
from rauth.service import OAuth1Service, OAuth1Session
import xml.etree.ElementTree as ET
#from lib import webbroswer as webbroswer
#import oauth2 as oauth
#import urlparse
url = 'http://www.goodreads.com'
request_token_url = '%s/oauth/request_token/' % url
authorize_url = '%s/oauth/authorize/' % url
access_token_url = '%s/oauth/access_token/' % url
goodreads = OAuth1Service(
consumer_key='REMOVED',
consumer_secret='REMOVED',
name='goodreads',
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...',
base_url='http://www.goodreads.com/'
)
# head_auth=True is important here; this doesn't work with oauth2 for some reason
request_token, request_token_secret = goodreads.get_request_token(header_auth=True)
authorize_url = goodreads.get_authorize_url(request_token)
os.startfile(authorize_url)
#webbrowser.open(authorize_url, new=0, autoraise=True)
print ('Visit this URL in your browser: ' + authorize_url)
accepted = 'n'
while accepted.lower() == 'n':
# you need to access the authorize_link via a browser,
# and proceed to manually authorize the consumer
accepted = input('Have you authorized me? (y/n) ')
session = goodreads.get_auth_session(request_token, request_token_secret)
useridxml = session.get('https://www.goodreads.com/api/auth_user')
root = ET.fromstring(useridxml.content)
for child in root:
if str(child.tag)=='user':
userid = (child.attrib['id'])
break
sessionparams = {'id':userid}
#print(type(session))
userfriendsxml = session.get('https://www.goodreads.com/friend/user...)
root = ET.fromstring(userfriendsxml.content)
print('reached here')
nodelist = root.findall('./friends/user/id')
print("Listing owned books by your friends on goodreads")
for each in nodelist:
print(each.text)
ownedbooksxml = session.get('https://www.goodreads.com/owned_books...', params={'id':each.text})
print(ownedbooksxml.content)
break
#for node in root:
# if(str(node.tag)=='friends'):
# for child in node:
# if(str(child.tag)=="user"):
# for grandchild in child:
# print(grandchild.text)
f = open('xmloutput.xml', 'w')
f.write(str(userfriendsxml.content))
# book_id 631932 is "The Greedy Python"
#data = {'id': 'to-read', 'book_id': 631932}
# add this to our "to-read" shelf
#response = session.post('http://www.goodreads.com/shelf/add_to...', data)
# these values are what you need to save for subsequent access.
#ACCESS_TOKEN = session.access_token
#ACCESS_TOKEN_SECRET = session.access_token_secret