加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – 使用httplib2错误的HTTP帖子

发布时间:2020-12-20 11:26:36 所属栏目:Python 来源:网络整理
导读:我正在尝试使用Google here所述的刷新令牌来获取新的访问令牌.Google说我需要发出HTTP请求.我不知道如何做到这一点,所以我从 here开始查找如何操作.但是,我必须正确地发布帖子,因为我收到invalid_request错误. 以下是我的相关代码: h = Http()post_data = {
我正在尝试使用Google here所述的刷新令牌来获取新的访问令牌.Google说我需要发出HTTP请求.我不知道如何做到这一点,所以我从 here开始查找如何操作.但是,我必须正确地发布帖子,因为我收到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))

我得到的回应是:

{
  "error" : "invalid_request"
}

我在这里错过了什么?

解决方法

当它应该在标题中发送时,它实际上就是你在正文中发送’Content-type’.此外,您的身体中不需要’POST’:’/ o / oauth2 / token HTTP / 1.1’和’HOST:’:’accounts.google.com’.试着这样做:

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

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

resp,urlencode(post_data),headers=headers)

print content

它应该打印如下:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读