1

以下内容在本地有效,但在部署时无效:

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)

它可能与这个问题有关:部署到 appengine 时的 freebase api 错误:DownloadError: ApplicationError: 2在这种情况下,向请求中添加 api 密钥可以修复它。虽然我尝试添加密钥(使用 urlfetch)导致错误:

{u'code': 100, u'message': u'Invalid API Key (Key not found)', u'stat': u'fail'}. 

我注册了 freebase 服务并将浏览器应用程序的简单 API 密钥添加到查询中:'&key=apikeystringhere'。我已将关键问题添加为单独的问题:Setup api key for freebase queries from appengine

4

2 回答 2

0

为什么不使用 urlfetch API 而不是 urllib2?

于 2012-05-31T03:50:21.807 回答
0

我现在可以工作了。它在遥控器上不起作用,因为我需要一把钥匙。我之前尝试添加密钥的尝试失败了,因为我在应该使用时使用了已弃用的http://api.freebase.com API:https ://www.googleapis.com/freebase/v1/search?q=bob&key =

于 2012-05-31T13:15:58.560 回答