使用google api刷新令牌,返回invalid_request [英] Refresh token using google api returning invalid_request

查看:202
本文介绍了使用google api刷新令牌,返回invalid_request的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用Google API刷新已过期的令牌,请遵循 https: //developers.google.com/accounts/docs/OAuth2WebServer#refresh



所以这里是我的代码(python)

  refresh_token = requests.post(
'https://accounts.google.com/o/oauth2/token',
headers = {
'Content-Type':'application / x-www-form-urlencoded',
},
data = json.dumps({$ b $'client_id':APP_ID,
'client_secret':APP_SECRET,
'refresh_token':refresh_token,
'grant_type':'refresh_token',
})

但是,我收到了回复:

 状态码:400 
{
错误:invalid_request
}

我在做什么错了?

你正在告诉谷歌服务器,你的请求是在其实际的json编码时进行表单编码的。如果你给它一个字典,请求会自动对你的数据进行形式编码,所以试试这个:

  refresh_token = requests.post( 
'https://accounts.google.com/o/oauth2/token',
data = {$ b $'client_id':APP_ID,
'client_secret':APP_SECRET,
'refresh_token':refresh_token,
'grant_type':'refresh_token',
}


I am trying to refresh my expired token using the Google API following the instructions on https://developers.google.com/accounts/docs/OAuth2WebServer#refresh

So here is my code (python)

refresh_token = requests.post(
    'https://accounts.google.com/o/oauth2/token',
     headers={
        'Content-Type': 'application/x-www-form-urlencoded',
     },
     data=json.dumps({
         'client_id': APP_ID,
         'client_secret': APP_SECRET,
         'refresh_token': refresh_token,
         'grant_type': 'refresh_token',
     })
)

However, I am getting the response:

Status code: 400
{
  "error" : "invalid_request"
}

What am I doing wrong?

解决方案

You are telling the google server that your request is form-encoded when its actually json encoded. Requests will automatically form-encode your data if you give it a dict, so try this instead:

refresh_token = requests.post(
    'https://accounts.google.com/o/oauth2/token',
     data={
         'client_id': APP_ID,
         'client_secret': APP_SECRET,
         'refresh_token': refresh_token,
         'grant_type': 'refresh_token',
     }
)

这篇关于使用google api刷新令牌,返回invalid_request的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆