GAE google books api

时间:2016-04-22 20:53:21

标签: python api google-app-engine google-books

我正在使用Python 2.7.11制作GAE应用。这是我构建GAE应用程序的第一个项目,我也是Web开发的新手。 Link here。我正在尝试访问Google图书的API以检索图书信息。在本地部署时,一切都完美无瑕。我在搜索字段中输入了一本书的名字,我的处理程序飞了,点击2个Google API(搜索,然后是音量),解析结果并将其附加到页面。

当我将我的应用程序部署到GAE时,我收到以下错误(带有堆栈跟踪):

   Traceback (most recent call last):
     File         "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1535, in __call__
    rv = self.handle_exception(request, response, e)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1529, in __call__
    rv = self.router.dispatch(request, response)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1102, in __call__
    return handler.dispatch()
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 572, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 570, in dispatch
    return method(*args, **kwargs)
  File "/base/data/home/apps/s~my-life-app/1.392279800807967905/myapp.py", line 320, in post
    response_body = urlopen(request).read()
  File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/urllib2.py", line 127, in urlopen
    return _opener.open(url, data, timeout)
  File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/urllib2.py", line 410, in open
    response = meth(req, response)
  File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/urllib2.py", line 523, in http_response
    'http', request, response, code, msg, hdrs)
  File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/urllib2.py", line 448, in error
    return self._call_chain(*args)
  File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/urllib2.py", line 382, in _call_chain
    result = func(*args)
  File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/urllib2.py", line 531, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error 403: Forbidden

以下是执行API调用的代码部分。 (它非常草率,抱歉。我在清理代码之前一直试图解决这个问题。)

  headers = {'Accept': 'application/json'}      
  book = Book(parent=books_key)
  book.name = self.request.get('name')
  googleBookSearch = "https://www.googleapis.com/books/v1/volumes?q="
  googleBookVol = "https://www.googleapis.com/books/v1/volumes/ID"
  escapedBookName = urllib.quote(book.name)
  apiCall = googleBookSearch + escapedBookName + "&" + api_key2
  request = Request(apiCall, headers=headers)
  response_body = urlopen(request).read()
  parsed_book = json.loads(response_body)
  if parsed_book['totalItems'] != 0:
    volumeID = parsed_book['items'][0]['id']
    googleBookVol = googleBookVol.replace("ID", volumeID)
    googleBookVol = googleBookVol + "?" + api_key2
    logging.info(googleBookVol)
    request = Request(googleBookVol, headers=headers)
    response_body = urlopen(request).read()
    response_body = json.loads(response_body)
    book.name = response_body['volumeInfo']['title']
    pageCount = response_body['volumeInfo']['pageCount']
    book.pages = int(pageCount)
    bookCover = response_body['volumeInfo']['imageLinks']['smallThumbnail']
    book.cover = str(bookCover)
    book.published = str(response_body['volumeInfo']['publishedDate'])
    book.author = str(response_body['volumeInfo']['authors'][0])
    book.put()
    self.redirect("/books")

根据Google的API文档,这些API仅访问公共数据,因此不需要OAuth或API密钥。不用说,那不起作用。我已经添加了我的API密钥,但仍然没有用。我已从开发者控制台启用了Google Books API。唯一留给我的是Oauth,但是我的脑袋正在旋转,试图阅读文档页面上的页面,其中一些页面彼此截然不同,所以我不知道要实现什么。 Web应用程序不需要任何类型的登录,API请求仅使用公共数据。感谢。

有没有人可以提供帮助?

编辑:这是我从Google控制台日志中提取的从谷歌回来的日志部分。第一部分是确切的URL,如logging.info。

所示
  

https://www.googleapis.com/books/v1/volumes?q=Pale+Blue+Dot&key=AIzaSyD9o4jKfQvvCAr8glvom4llEAssu8ojmgk

{
metadata:
{
severity:
"ERROR"
projectId:
"598422355661"
serviceName:
"appengine.googleapis.com"
zone:
"us6"
labels:
{…}
timestamp:
"2016-04-23T17:40:38.988615Z"
projectNumber:
"598422355661"
}
protoPayload:
{
@type:
"type.googleapis.com/google.appengine.logging.v1.RequestLog"
appId:
"s~my-life-app"
versionId:
"1"
requestId:
"571bb39600ff0f15c71037dd9b0001737e6d792d6c6966652d617070000131000100"
ip:
"189.61.48.66"
startTime:
"2016-04-23T17:40:38.988615Z"
endTime:
"2016-04-23T17:40:39.020226Z"
latency:
"0.031611s"
megaCycles:
"20"
method:
"POST"
resource:
"/sign"
httpVersion:
"HTTP/1.1"
status:
500
responseSize:
"870"
referrer:
"http://my-life-app.appspot.com/books"
userAgent:
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36"
urlMapEntry:
"myapp.app"
host:
"my-life-app.appspot.com"
cost:
1.29081e-7
instanceIndex:
-1
instanceId:
"00c61b117cb28d6a28da9eadf0f9ad4279a74be43a1ca345cb"
line:
[
0:
{
time:
"2016-04-23T17:40:38.991900Z"
severity:
"INFO"
logMessage:
"https://www.googleapis.com/books/v1/volumes?q=Pale+Blue+Dot&key=AIzaSyD9o4jKfQvvCAr8glvom4llEAssu8ojmgk"
}
1:
{
time:
"2016-04-23T17:40:39.011339Z"
severity:
"ERROR"
logMessage:
"HTTP Error 403: Forbidden
Traceback (most recent call last):
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1535, in __call__
    rv = self.handle_exception(request, response, e)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1529, in __call__
    rv = self.router.dispatch(request, response)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1102, in __call__
    return handler.dispatch()
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 572, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 570, in dispatch
    return method(*args, **kwargs)
  File "/base/data/home/apps/s~my-life-app/1.392300489206286534/myapp.py", line 333, in post
    response_body = urlopen(request).read()
  File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/urllib2.py", line 127, in urlopen
    return _opener.open(url, data, timeout)
  File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/urllib2.py", line 410, in open
    response = meth(req, response)
  File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/urllib2.py", line 523, in http_response
    'http', request, response, code, msg, hdrs)
  File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/urllib2.py", line 448, in error
    return self._call_chain(*args)
  File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/urllib2.py", line 382, in _call_chain
    result = func(*args)
  File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/urllib2.py", line 531, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error 403: Forbidden"
}
]
appEngineRelease:
"1.9.36"
}
insertId:
"2016-04-23|10:40:40.411640-07|10.106.197.137|1661671430"
log:
"appengine.googleapis.com/request_log"
httpRequest:
{
status:
500
}
operation:
{
id:
"571bb39600ff0f15c71037dd9b0001737e6d792d6c6966652d617070000131000100"
producer:
"appengine.googleapis.com/request_id"
}
}

1 个答案:

答案 0 :(得分:1)

对于未来的挣扎者,请务必在请求中添加相应的国家/地区字符串

&country=US

  

Books API使用客户端的IP地址对用户进行地理定位。由于我们必须遵守各国的版权法,并且拥有发布商的国家/地区特定权利,因此我们需要知道请求所在的国家/地区,以便提供适当的内容。

source