简体   繁体   中英

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. In the Developers console, I have the URL Shortener API turned on, and oAuth 2 is also turned on. On top of that I have the simple API Access Browser key obtained from the API Access screen.

Here is the problem. When I run the following code, I get "HTTPError: HTTP Error 403: Forbidden" in the Developers Console log. 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,

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

Prod and Dev both return HTTP Error 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.

  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.

  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)

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


Dev with API Key

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

Google has a nice API for this. You can test your requests here . Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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