Question

import httplib2
from urllib import urlencode
h = httplib2.Http()
h.add_credentials('zackster@gmail.com', 'PassWord')
data = dict(key="ThisIsMyApiKeyICopiedAndPastedIt")
resp, content = h.request("https://www.googleapis.com/calendar/v3/users/me/calendarList", "GET", urlencode(data), headers={'content-type':'text/plain'})



>>> resp
{'date': 'Wed, 13 Jun 2012 04:33:03 GMT', 'status': '400', 'content-length': '925', 'content-type': 'text/html; charset=UTF-8', 'server': 'GFE/2.0'}

>>> print content

<!DOCTYPE html>
<html lang=en>
  <meta charset=utf-8>
  <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
  <title>Error 400 (Bad Request)!!1</title>
  <style>
    *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}
  </style>
  <a href=//www.google.com/><img src=//www.google.com/images/errors/logo_sm.gif alt=Google></a>
  <p><b>400.</b> <ins>That’s an error.</ins>
  <p>Your client has issued a malformed or illegal request.  <ins>That’s all we know.</ins>
Was it helpful?

Solution

I'm guessing, but you are sending your API key as POST data. It needs to be appended to the URL as a query string. This is the HTTP request generated by your code:

GET /calendar/v3/users/me/calendarList HTTP/1.1
Host: www.googleapis.com
Content-Length: 36
content-type: text/plain
accept-encoding: gzip, deflate
user-agent: Python-httplib2/$Rev$

key=ThisIsMyApiKeyICopiedAndPastedIt

Rather (if my guess is right) it should be:

GET /calendar/v3/users/me/calendarList?key=ThisIsMyApiKeyICopiedAndPastedIt HTTP/1.1
Host: localhost:1234
content-type: text/plain
accept-encoding: gzip, deflate
user-agent: Python-httplib2/$Rev$

So your code should be modified to something like this:

url = 'https://www.googleapis.com/calendar/v3/users/me/calendarList'
uri = '%s?%s' % (url, urlencode(data))
resp, content = h.request(uri, "GET", headers={'content-type':'text/plain'})
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top