2013-04-10 98 views
2

我试图通过使用谷歌here所述的刷新令牌来获得新的访问令牌。 Google说我需要提出HTTP请求。我不知道该怎么做,所以我查了一下如何从here这样做。但是,我必须错误地发帖,因为我得到了一个invalid_request错误。HTTP Post使用httplib2错误

下面是我的相关代码:

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

而我得到的回应是:

{ 
    "error" : "invalid_request" 
} 

缺少什么我在这里?

+0

这链路使用'httplib2'但你'urllib2'标记。你有没有安装'httplib2'?你在用哪个? – Jared 2013-04-10 01:15:36

+0

Woops,这是一个错字。我正在使用'httplib2'。感谢您指出了这一点。 – LucasS 2013-04-10 01:17:34

回答

6

这真的只是你发送'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 

应该打印出类似这样:

{ 
    "access_token" : "ya29.AHBS6ZCtS8mBc_vEC9FFBkW2x3ipa7FLOs-Hi-3UhVkpacOm", 
    "token_type" : "Bearer", 
    "expires_in" : 3600 
} 
+0

工程就像一个魅力,感谢的人。 – LucasS 2013-04-10 01:55:06