我正在使用Python 2.7.11製作GAE應用程序。這是我第一個構建GAE應用程序的項目,我也是web開發新手。 Link here。我正嘗試訪問Google圖書的API以檢索圖書信息。當本地部署時,一切都完美無瑕。我在搜索字段中輸入了一本書的名字,我的處理程序飛過來,命中2個Google API(搜索,然後是音量),分析結果並將其附加到頁面。GAE谷歌圖書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從雲端控制檯日誌中取回的內容的日誌部分。第一部分是由logging.info顯示的確切URL。
{
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"
}
}
你能提供來自谷歌圖書API充分反應? – Tijkijiki
剛剛做到了。乾杯。 –
謝謝,嘗試在您的查詢字符串中添加「&country = US」(或您所在的國家/地區)。 – Tijkijiki