来自Python AppEngine的Google Url Shortener API:HTTPError:HTTP错误403:禁止

时间:2014-07-19 21:14:47

标签: python api google-app-engine

我在AppEngine生产环境中使用Google URL Shortener API时遇到问题。 在开发人员控制台中,我打开了URL Shortener API,并且oAuth 2也已打开。最重要的是,我从API Access屏幕获得了简单的API Access Browser密钥。

这是问题所在。当我运行以下代码时,我得到" HTTPError:HTTP错误403:禁止"在开发人员控制台日志中。有趣的是,相同的代码正确地返回了开发环境中的短URL。

def goo_shorten_url(url):

  post_url = 'https://www.googleapis.com/urlshortener/v1/url?fields=id'
  logging.info('post_url: {}'.format(post_url))
  postdata = {'longUrl':url}
  headers = {'Content-Type':'application/json'}
  req = urllib2.Request(
    post_url,
    json.dumps(postdata),
    headers
)
ret = urllib2.urlopen(req).read()
print ret
return json.loads(ret)['id']

如果我将API密钥包含在帖子网址中,如下所示

post_url = 'https://www.googleapis.com/urlshortener/v1/url?fields=id&key=MYAPIKEY'

Prod和Dev都返回HTTP错误403。

我怀疑这三个中的一个是真的,但我想听听你的想法。

  1. 需要API密钥,但我没有使用正确的API密钥。

  2. 不需要API密钥(这解释了它在Dev中没有密钥的原因),但我的API密钥错误导致Prod和Dev失败。

  3. Google不允许应用程序以编程方式向其Url缩短程序API提交POST请求。(这并不能解释为什么它可以在Dev中运行)

  4. 感谢阅读。


    PROD

    文件" /base/data/home/apps/s~myapp/1.377367579804576653/util/test_module.py",第50行,获取     strin = goo_shorten_url(longurl)   文件" /base/data/home/apps/s~myapp/1.377367579804576653/util/JOTools.py",第41行,goo_shorten_url     ret = urllib2.urlopen(req).read()   文件" /base/data/home/runtimes/python27/python27_dist/lib/python2.7/urllib2.py" ;,第127行,在urlopen中     return _opener.open(url,data,timeout)   文件" /base/data/home/runtimes/python27/python27_dist/lib/python2.7/urllib2.py" ;,第410行,处于打开状态     response = meth(req,response)   文件" /base/data/home/runtimes/python27/python27_dist/lib/python2.7/urllib2.py",第523行,在http_response中     ' http',请求,响应,代码,消息,hdrs)   File" /base/data/home/runtimes/python27/python27_dist/lib/python2.7/urllib2.py" ;,第448行,出错     return self._call_chain(* args)   文件" /base/data/home/runtimes/python27/python27_dist/lib/python2.7/urllib2.py",第382行,在_call_chain中     result = func(* args)   文件" /base/data/home/runtimes/python27/python27_dist/lib/python2.7/urllib2.py",第531行,http_error_default     提出HTTPError(req.get_full_url(),code,msg,hdrs,fp) HTTPError:HTTP错误403:禁止


    使用API​​密钥开发

    文件" C:_dev \ eclipse-work \ gae \ MyProj \ util \ test_module.py",第50行,获取     strin = goo_shorten_url(longurl)   文件" C:_dev \ eclipse-work \ gae \ MyProj \ util \ JOTools.py",第41行,goo_shorten_url     ret = urllib2.urlopen(req).read()   文件" C:\ PYTHON27 \ lib \ urllib2.py",第127行,在urlopen中     return _opener.open(url,data,timeout)   文件" C:\ PYTHON27 \ lib \ urllib2.py",410行,打开     response = meth(req,response)   文件" C:\ PYTHON27 \ lib \ urllib2.py",第523行,在http_response中     ' http',请求,响应,代码,消息,hdrs)   文件" C:\ PYTHON27 \ lib \ urllib2.py",第448行,出错     return self._call_chain(* args)   文件" C:\ PYTHON27 \ lib \ urllib2.py",第382行,_call_chain     result = func(* args)   文件" C:\ PYTHON27 \ lib \ urllib2.py",第531行,http_error_default     提出HTTPError(req.get_full_url(),code,msg,hdrs,fp) HTTPError:HTTP错误403:禁止

1 个答案:

答案 0 :(得分:1)

谷歌有一个很好的API。您可以测试您的请求here。希望这可以帮助。