【发布时间】:2015-06-27 07:01:56
【问题描述】:
我正在使用 Google Books API 与我正在开发的电子商务网站集成。想法是让用户使用搜索栏搜索书籍,然后调用Google API输出搜索关键字对应的书籍数量。
但是,在表单中输入查询后单击提交后,我收到 403 Forbidden 错误。这很奇怪,因为这在我在 localhost 上测试我的应用程序时从未发生过。这是我的应用程序的代码:
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)
搜索.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>
现在,jsony.html 仍然不完整。我现在所做的只是显示包含原始未处理形式的输出 json 的 URL。
在我部署应用程序后出现此 403 错误的原因似乎是什么?
编辑 1:
当我从我的主要 python 文件中删除以下行时,问题解决了:
f = urllib2.urlopen(finaal).read()
但是,我需要 API 的 URL 才能从其源代码中提取数据。发生了什么事?
【问题讨论】:
-
你有没有去
https://console.developers.google.com/project/apps~YOUR-APP/apiui/api开启Books API? -
是的,我做到了。仍然,同样的错误。
-
作为一个单独的问题,您永远不会使用
f = ...。您只是将finaal字符串发送到模板。你确定你有正确的key吗?尝试在浏览器中直接访问该网址。 -
是的,我可以通过直接访问浏览器上的链接来访问 JSON 格式。当我向他们发送请求时,Google Books API 似乎禁止我访问他们的页面。
-
在您尝试 urlopen 之前登录
finaal,以确保它是您认为的那样
标签: python google-app-engine google-api