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>
问问题
1302 次
1 回答
1
我猜,但您将 API 密钥作为 POST 数据发送。它需要作为查询字符串附加到 URL。这是您的代码生成的 HTTP 请求:
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
相反(如果我的猜测是正确的)它应该是:
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$
所以你的代码应该修改成这样:
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'})
于 2012-06-13T07:54:14.897 回答