HTTP帖子使用httplib2错误

Luc*_*asS 3 python http-post httplib2 google-drive-api

我正在尝试使用Google 在此处描述的刷新令牌来获取新的访问令牌.Google说我需要发出HTTP请求.我不知道怎么做,所以我从这里查看了如何做到这一点.但是,我必须正确地发布帖子,因为我收到invalid_request错误.

以下是我的相关代码:

h = Http()
post_data = {'POST': '/o/oauth2/token HTTP/1.1', 
             'HOST:': 'accounts.google.com', 
             'Content-Type:': 'application/x-www-form-urlencoded',
             'client_id':ClientID,
             'client_secret':ClientSecret,
             'refresh_token':SavedRefreshToken,
             'grant_type':'refresh_token'}
resp, content = h.request("https://accounts.google.com/o/oauth2/token", 
                          "POST", 
                          urlencode(post_data))
Run Code Online (Sandbox Code Playgroud)

我得到的回应是:

{
  "error" : "invalid_request"
}
Run Code Online (Sandbox Code Playgroud)

我在这里错过了什么?

Jar*_*red 7

'Content-type'当它应该在标题中发送时,它真的只是你在身体中发送.此外,你不需要'POST': '/o/oauth2/token HTTP/1.1''HOST:': 'accounts.google.com'你的身体.试着这样做:

h = Http()
post_data = {'client_id':ClientID,
             'client_secret':ClientSecret,
             'refresh_token':SavedRefreshToken,
             'grant_type':'refresh_token'}

headers = {'Content-type': 'application/x-www-form-urlencoded'}

resp, content = h.request("https://accounts.google.com/o/oauth2/token", 
                          "POST", 
                          urlencode(post_data),
                          headers=headers)

print content
Run Code Online (Sandbox Code Playgroud)

它应该打印如下:

{
  "access_token" : "ya29.AHBS6ZCtS8mBc_vEC9FFBkW2x3ipa7FLOs-Hi-3UhVkpacOm",
  "token_type" : "Bearer",
  "expires_in" : 3600
}
Run Code Online (Sandbox Code Playgroud)