Goodreads Developers discussion

33 views
questions > Python OAUTH Retrieval - "String Indices Must Be Integers"

Comments Showing 1-5 of 5 (5 new)    post a comment »
dateUp arrow    newest »

message 1: by Systems (new)

Systems Lib | 6 comments I'm having a bit of trouble with the API - I've gotten to where I'm getting a response, but I keep getting an error cf. the string indices. The app is designed (theoretically) to allow people to import their owned books from Goodreads to a library reading list:

def goodReadsOAUTH_handler(request):
if request.method == "GET" and ('authorize' in request.GET):
if request.GET['authorize'] == '1':
try:
grClient.session.oauth_finalize()
except:
raise Http404("Finalizing the OAUTH failed")
try:
gc_user = grClient.auth_user()
except:
raise Http404("No User Found on Goodreads")
#try:
user_owned_books = gc_user.owned_books(page=1)
for gc_books in user_owned_books:
a_book = grClient.book(gc_books.gid)
book_author = grClient.author(a_book.authors[0].name)
temp_slug = slugify(book_author.name)
author_id = ItemAuthor.objects.filter(authorslug=temp_slug)
if not author_id:
author_id = ItemAuthor(author=book_author.name,authorslug=temp_slug)
author_id.save()
title_id = ItemTitle(author=author_id,title=a_book.title,rating=gc_book.review.rating)
title_id.save()
else:
author_id = author_id[0]
title_info = ItemTitle.objects.filter(author=author_id, title=a_book.book.title)
if not title_info:
title_id = ItemTitle(author=author_id,title=a_book.book.title,rating=a_book.review.rating)
title_id.save()
else:
title_id = title_info[0]
title_id.save()
temp.author = author_id
temp.title = title_id
temp.save()
setRating(title_id)
form.save_m2m()
UpdateTags(temp)
return HttpResponseRedirect(reverse('workroom:success', kwargs={'user':request.user.username,'type':'add'}))
return HttpResponseRedirect(reverse('workroom:profile',kwargs={'user':request.user.username}))
#except:
#raise Http404('No books owned.')
else:
raise Http404("OAUTH request not authorized")
else:
raise Http404("OAUTH failure.")

I'm using the goodreads Python wrapper v0.2.4


message 2: by Jeff (new)

Jeff (jeffwong) | 75 comments Mod
Are your string indicies not integers? This sounds like an error check in the library you are using.


message 3: by Systems (new)

Systems Lib | 6 comments As far as I can tell, my string indices are integers - the only thing I'm using that isn't generated from either the GoodReads API or the library is a 0.

It's looking like the issue is in the library itself; when I run the commands via shell, any attempt to retrieve data from an owned_book instance results in the "string indices not integers" error, even when there's no argument sent.


message 4: by Systems (new)

Systems Lib | 6 comments I think I may have found the issue:

The gc_user = grClient.auth_user() is returning

Error in formatting: TypeError: __repr__ returned non-string (type NoneType)


message 5: by Systems (last edited Nov 03, 2015 08:36AM) (new)

Systems Lib | 6 comments Okay!

After poking and prodding the API, I now have the error narrowed down to an issue when I retrieve an individual owned book. When I attempt to access the associated "book" within the API, that's when it tells me the string indices must be an integer.

After some more poking, I found the Last Usable Point; when the user_owned_books variable is created, it contains something akin to the following:

[goodreads.owned_book.GoodreadsOwnedBook instance at 0x40d0c20, goodreads.owned_book.GoodreadsOwnedBook instance at 0x3f5d200, goodreads.owned_book.GoodreadsOwnedBook instance at 0x4238098, goodreads.owned_book.GoodreadsOwnedBook instance at 0x42382d8, goodreads.owned_book.GoodreadsOwnedBook instance at 0x42383b0, goodreads.owned_book.GoodreadsOwnedBook instance at 0x4319998, goodreads.owned_book.GoodreadsOwnedBook instance at 0x4319a28, goodreads.owned_book.GoodreadsOwnedBook instance at 0x4319f38, goodreads.owned_book.GoodreadsOwnedBook instance at 0x4319e18]

However, any attempts to access these are treated as invalid, whether within a for-loop or directly via user_owned_books[0].

The wrapper is accessing the owned books as follows:

def owned_books(self, page=1):
"""Return the list of books owned by the user"""
resp = self._client.session.get("owned_books/user/%s.xml" % self.gid,
{'page': page, 'format': 'xml'})
return [owned_book.GoodreadsOwnedBook(d)
for d in resp['owned_books']['owned_book']]

Is this correct/best practice for creating a list of owned books from the API?


back to top