以下内容适用于本地但不适用于部署时:
import urllib
import urllib2
import simplejson as json
url = 'https://www.googleapis.com/rpc'
requests = [{
'method': 'freebase.text.get',
'apiVersion': 'v1',
'params': {
'id': ['en','bob_dylan']
}
},{
'method': 'freebase.text.get',
'apiVersion': 'v1',
'params': {
'id': ['en','blade_runner']
}
}]
headers = { 'Content-Type': 'application/json' }
req = urllib2.Request(url, json.dumps(requests), headers)
response = urllib2.urlopen(req)
print response.read()
部署时会抛出以下错误:
class 'urllib2.HTTPError': HTTP Error 404: Not Found
Traceback (most recent call last):
File "/base/data/home/apps/s~34thquote/1.359254037177435492/test.py", line 38, in <module>
response = urllib2.urlopen(req)
File "/base/python_runtime/python_dist/lib/python2.5/urllib2.py", line 124, in urlopen
return _opener.open(url, data)
File "/base/python_runtime/python_dist/lib/python2.5/urllib2.py", line 387, in open
response = meth(req, response)
File "/base/python_runtime/python_dist/lib/python2.5/urllib2.py", line 498, in http_response
'http', request, response, code, msg, hdrs)
File "/base/python_runtime/python_dist/lib/python2.5/urllib2.py", line 425, in error
return self._call_chain(*args)
File "/base/python_runtime/python_dist/lib/python2.5/urllib2.py", line 360, in _call_chain
result = func(*args)
File "/base/python_runtime/python_dist/lib/python2.5/urllib2.py", line 506, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
它可能与此问题有关:freebase api error on deployment to appengine: DownloadError: ApplicationError: 2在这种情况下,为请求添加api密钥可以解决它。虽然我尝试添加密钥(使用urlfetch)导致错误:
{u'code': 100, u'message': u'Invalid API Key (Key not found)', u'stat': u'fail'}.
我注册了freebase服务,并将浏览器应用的简单API密钥添加到查询中:'&amp; key = apikeystringhere'。我已将关键问题添加为单独的问题:Setup api key for freebase queries from appengine
答案 0 :(得分:0)
为什么不使用urlfetch API而不是urllib2?
答案 1 :(得分:0)
我现在正在工作。它不能在遥控器上工作,因为我需要一把钥匙。我之前尝试添加密钥失败了,因为我在使用时使用了已弃用的http://api.freebase.com API:https://www.googleapis.com/freebase/v1/search?q=bob&key=