简体   繁体   English

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

[英]Google Url Shortener API from Python AppEngine: HTTPError: HTTP Error 403: Forbidden

I'm having trouble using Google URL Shortener API in AppEngine production environment. 我在AppEngine生产环境中使用Google URL Shortener API时遇到问题。 In the Developers console, I have the URL Shortener API turned on, and oAuth 2 is also turned on. 在开发者控制台中,我启用了URL Shortener API,并且还打开了oAuth 2。 On top of that I have the simple API Access Browser key obtained from the API Access screen. 最重要的是,我具有从API Access屏幕获得的简单API Access Browser键。

Here is the problem. 这是问题所在。 When I run the following code, I get "HTTPError: HTTP Error 403: Forbidden" in the Developers Console log. 当我运行以下代码时,在开发人员控制台日志中会收到“ HTTPError:HTTP错误403:禁止访问”。 Interestingly, the same code properly returns the short url in the development environment. 有趣的是,相同的代码可以在开发环境中正确返回短网址。

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']

If I include the API key in the post url as follows, 如果我将API密钥包含在发布网址中,如下所示,

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

Prod and Dev both return HTTP Error 403. Prod和Dev都返回HTTP错误403。

I suspect one of these three is true, but would like to hear your thoughts. 我怀疑这三个是正确的,但想听听您的想法。

  1. An API key is required, but I'm not using the right API key. API密钥是必需的,但是我没有使用正确的API密钥。

  2. An API key is not required (which explains why it work with no key in Dev), but my API key is wrong resulting both Prod and Dev fail. 不需要API密钥(这说明了为什么它在Dev中没有密钥的原因),但是我的API密钥是错误的,导致Prod和Dev均失败。

  3. Google doesn't allow applications to programmatically submit a POST request to its Url shortener API.(this doesn't explain why it would work in Dev at all) Google不允许应用程序以编程方式向其网址缩短工具API提交POST请求(这根本无法解释为什么它可以在Dev中使用)

Thanks for reading. 谢谢阅读。


Prod

File "/base/data/home/apps/s~myapp/1.377367579804576653/util/test_module.py", line 50, in get strin = goo_shorten_url(longurl) File "/base/data/home/apps/s~myapp/1.377367579804576653/util/JOTools.py", line 41, in goo_shorten_url ret = urllib2.urlopen(req).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/l 在get strin中,文件“ /base/data/home/apps/s~myapp/1.377367579804576653/util/test_module.py”,第50行= 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中返回_opener.open(URL,数据,超时)文件“ /base/data/home/runtimes/python27/python27_dist/lib/python2.7/urllib2.py”,第410行,在打开响应中= meth(req,response)文件“ /base/data/home/runtimes/python27/python27_dist/lib/python2.7/urllib2.py”,第523行,位于http_response'http'中,请求,响应,代码,msg, hdrs)文件“ /base/data/home/runtimes/python27/python27_dist/lib/python2.7/urllib2.py”,行448,错误返回self._call_chain(* args)文件“ / base / data / home / _call_chain中的runtimes / python27 / python27_dist / lib / python2.7 / urllib2.py“行382 = func(* args)文件” / base / data / home / runtimes / python27 / python27_dist / l ib/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 ib / python2.7 / urllib2.py“,第531行,位于http_error_default中,引发HTTPError(req.get_full_url(),代码,msg,hdrs,fp)HTTPError:HTTP错误403:禁止


Dev with API Key 具有API密钥的开发

File "C:_dev\\eclipse-work\\gae\\MyProj\\util\\test_module.py", line 50, in get strin = goo_shorten_url(longurl) File "C:_dev\\eclipse-work\\gae\\MyProj\\util\\JOTools.py", line 41, in goo_shorten_url ret = urllib2.urlopen(req).read() File "C:\\PYTHON27\\lib\\urllib2.py", line 127, in urlopen return _opener.open(url, data, timeout) File "C:\\PYTHON27\\lib\\urllib2.py", line 410, in open response = meth(req, response) File "C:\\PYTHON27\\lib\\urllib2.py", line 523, in http_response 'http', request, response, code, msg, hdrs) File "C:\\PYTHON27\\lib\\urllib2.py", line 448, in error return self._call_chain(*args) File "C:\\PYTHON27\\lib\\urllib2.py", line 382, in _call_chain result = func(*args) File "C:\\PYTHON27\\lib\\urllib2.py", line 531, in http_error_default raise HTTPError(req.get_full_url(), code, msg, hdrs, fp) HTTPError: HTTP Error 403: Forbidden 在get strin = goo_shorten_url(longurl)的第50行中,文件“ C:_dev \\ eclipse-work \\ gae \\ MyProj \\ util \\ test_module.py”文件“ 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中返回_opener.open(URL,数据,超时) http_response'http'中的文件“ C:\\ PYTHON27 \\ lib \\ urllib2.py”,第410行,打开响应= meth(req,response)文件“ C:\\ PYTHON27 \\ lib \\ urllib2.py”,第523行,请求,响应,代码,msg,hdrs)文件“ C:\\ PYTHON27 \\ lib \\ urllib2.py”,行448,错误返回self._call_chain(* args)文件“ C:\\ PYTHON27 \\ lib \\ urllib2.py” _call_chain中的第382行结果= func(* args)文件“ C:\\ PYTHON27 \\ lib \\ urllib2.py”,第531行在http_error_default中引发HTTPError(req.get_full_url(),代码,msg,hdrs,fp)HTTPError :HTTP错误403:禁止

Google has a nice API for this. Google为此提供了一个不错的API You can test your requests here . 您可以在此处测试您的请求。 Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM