Google Buzz API - 无法解决错误:"缺少必要参数 - grant_type
我正在尝试用Python制作一个桌面应用程序,目的是向Google Buzz发送消息。不过,我在获取代码时遇到了一些问题。这段代码是用户在授权应用后需要从浏览器中复制粘贴的,但我却无法用它来换取访问令牌(access_token)。
我正在向www.google.com4/accounts/o8/oauth2/token发送一个POST请求,具体方法如下:
send: 'POST /accounts/o8/oauth2/token HTTP/1.1\r\nHost: www.google.com\r\nAccept-Encoding: identity\r\nContent-Length: 194\r\n\r\nclient_id=XXXXXXX.apps.googleusercontent.com&client_secret=XXXXXXXXXXXXXX&code=4/JFxWkTynH63BruP_dLqtJTV0O_oc&grant_type=authorization_code&redirect_uri=urn:ietf:wg:oauth:2.0:oob'
reply: 'HTTP/1.1 400 Required parameter is missing: grant_type\r\n'
header: Cache-Control: no-cache, no-store, max-age=0, must-revalidate
header: Pragma: no-cache
header: Expires: Fri, 01 Jan 1990 00:00:00 GMT
header: Date: Mon, 30 May 2011 09:04:36 GMT
header: Content-Type: text/html; charset=UTF-8
header: X-Content-Type-Options: nosniff
header: X-Frame-Options: SAMEORIGIN
header: X-XSS-Protection: 1; mode=block
header: Server: GSE
header: Transfer-Encoding: chunked
400 Required parameter is missing: grant_type
<HTML>
<HEAD>
<TITLE>Required parameter is missing: grant_type</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Required parameter is missing: grant_type</H1>
<H2>Error 400</H2>
</BODY>
</HTML>
更具体地说,我是这样发送参数的(从上面复制粘贴并格式化):
POST /accounts/o8/oauth2/token HTTP/1.1\r\n
Host: www.google.com\r\n
Accept-Encoding: identity\r\n
Content-Length: 194\r\n
\r\n
client_id=XXXXXXX.apps.googleusercontent.com&
client_secret=XXXXXXXXXXXXXX&
code=4/JFxWkTynH63BruP_dLqtJTV0O_oc&
grant_type=authorization_code&
redirect_uri=urn:ietf:wg:oauth:2.0:oob
有没有什么想法、建议或指点?我该如何修复这个错误?我哪里做错了吗?
谢谢你的时间和耐心。
最好的祝福,
Srivatsan Iyer
1 个回答
1
我来回答我的问题,希望能帮助到未来的读者。
我成功解决了我的问题。我用Python的 HTTPSConnection
发送了上面的数据,但是在GAE(谷歌应用引擎)上可能不太适用。于是我改用了 google.appengine.api.urlfetch
,结果一切都正常了!:-)