【问题标题】:Refresh token using google api returning invalid_request使用返回 invalid_request 的谷歌 api 刷新令牌
【发布时间】:2014-03-31 01:50:18
【问题描述】:

我正在尝试按照https://developers.google.com/accounts/docs/OAuth2WebServer#refresh 上的说明使用 Google API 刷新我过期的令牌

这是我的代码(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',
     })
)

但是,我收到了回复:

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

我做错了什么?

【问题讨论】:

    标签: python rest google-api access-token


    【解决方案1】:

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

    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',
         }
    )
    

    【讨论】:

    • 谢谢!没想到是头问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-19
    • 2021-05-06
    • 1970-01-01
    • 2013-07-20
    • 2020-07-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多