【问题标题】:How to update access token using refresh token in you tube api?如何使用 youtube api 中的刷新令牌更新访问令牌?
【发布时间】:2015-02-27 13:17:28
【问题描述】:

我已经尝试过这个来更新我的访问令牌

import urllib
endpoint='https://accounts.google.com/o/oauth2/token'
data={'client_id':'25********15-6*********************7f.apps.googleusercontent.com','client_secret':'4********Pj-K*****x4aM','refresh_token':'1/tP************************O_XclU','grant_type':'refresh_token'}
encodedData=urllib.urlencode(data)
from httplib2 import Http
h = Http()
resp, content = h.request(endpoint, "POST", encodedData)

但收到错误消息

'{\n  "error" : "invalid_request",\n  "error_description" : "Required parameter is missing: grant_type"\n}'

【问题讨论】:

    标签: python post youtube-api


    【解决方案1】:

    您应该像这样在请求中指定标头:

    resp, content = h.request(uri=endpoint,
                              method="POST", 
                              body=encodedData, 
                              headers={'Content-type': 'application/x-www-form-urlencoded'})
    

    【讨论】:

      【解决方案2】:

      old-refresh_token 是您拥有的刷新令牌

      CLIENT_ID,CLIENT_SECRET 是您可以在谷歌开发者控制台中找到的凭据

      http = httplib2.Http()
      TOKEN_URL = "https://accounts.google.com/o/oauth2/token"
      
      headers = {'Content-Type': 'application/x-www-form-urlencoded'}
      parameters = urllib.urlencode({'refresh_token': old-refresh_token, 'client_id': CLIENT_ID, 'client_secret': CLIENT_SECRET, 'grant_type': 'refresh_token'})
      resp, response_token = http.request(TOKEN_URL, method='POST', body=parameters, headers=headers)
      token_data = json.loads(response_token)
      access_token = token_data['access_token']
      

      变量 access_token 现在保存您的访问令牌

      试试看

      【讨论】:

        猜你喜欢
        • 2014-09-13
        • 2020-07-12
        • 2016-08-13
        • 2017-08-11
        • 2022-12-23
        • 2019-06-29
        • 2023-04-01
        • 2013-08-28
        • 1970-01-01
        相关资源
        最近更新 更多