2

I'm using Google Books API to integrate with an E-commerce site that I'm developing. The idea is to let the users search for books using a search bar, and then calling the Google API to output the number of books corresponding to the search keyword.

However, I get a 403 Forbidden error after I click submit after entering my query in the form. This is strange, because this never happened when I was testing my application on the localhost. Here's the code for my application:

main.py

class SearchHandler(Handler):
    def get(self):
        self.render("search.html")
    def post(self):
        keey = self.request.get('keey')
        finaal = "https://www.googleapis.com/books/v1/volumes?q=" + keey + "&key=MY_APP_KEY"
        f = urllib2.urlopen(finaal).read()
        self.render("jsony.html", finaal = finaal)

app = webapp2.WSGIApplication(('/search', SearchHandler)], debug=True)

search.html

<html>
<head>
    <title>Web Mining</title>
</head>
<body>
    <form method = "post">
        Book Name:<input type = "text" name = "keey">
        <input type = "submit">
    </form>
</body>
</html> 

jsony.html

<html>
<head>
    <title>Web Mining</title>
</head>
<body>
    <form method = "post">
        {{finaal}}
    </form>
</body>

Now, the jsony.html is still incomplete. All I'm doing now is displaying the URL which contains the outputted json in it's raw, unprocessed form.

What seems to be causing this 403 error to arise after I deploy my application ?

EDIT 1:

The problem resolves when I remove the following line from my main python file:

f = urllib2.urlopen(finaal).read()

However, I would be needing my API's URL in order to extract data from its source code. What's happening ?

Manas Chaturvedi
  • 5,210
  • 18
  • 52
  • 104
  • Did you go to `https://console.developers.google.com/project/apps~YOUR-APP/apiui/api` and turn on the Books API? – GAEfan Aug 20 '14 at 01:44
  • Yes I did. Still, the same error. – Manas Chaturvedi Aug 20 '14 at 03:19
  • As a separate issue, you never use the `f = ...`. You are just sending the `finaal` string to the template. Are you sure you have the right `key`? try going directly to that url in your browser. – GAEfan Aug 20 '14 at 04:27
  • Yes, I can access the JSON format by directly accessing the link on my browser. It seems that Google Books API is forbidding me to access their page when I send them a request. – Manas Chaturvedi Aug 20 '14 at 04:31
  • log `finaal` just before you try to urlopen it, to make sure it is what you think it is – GAEfan Aug 20 '14 at 04:36
  • I realized that Google Books API is blocking my request when I host my application online. However, it works just fine when I run my application on the local host. Is there no way to let the API know that my request is not coming from a bot? – Manas Chaturvedi Aug 23 '14 at 10:48
  • Looks similar to [urllib2.HTTPError: HTTP Error 403: Forbidden](https://stackoverflow.com/questions/13303449/urllib2-httperror-http-error-403-forbidden/46213623#46213623) – Supreet Sethi Nov 06 '17 at 17:59

1 Answers1

1

Try adding &country=US to the URL query string.

Bill Prin
  • 2,498
  • 1
  • 20
  • 27