【问题标题】:google-python-api-client refresh token is nullgoogle-python-api-client 刷新令牌为空
【发布时间】:2018-11-19 07:21:13
【问题描述】:

我正在尝试将刷新令牌与 google-python-api-client 一起使用,以避免用户每次都对应用进行身份验证。

我有以下代码通过让用户每次都进行身份验证来构建健身服务

from apiclient.discovery import build
from oauth2client import tools
from oauth2client.file import Storage
from oauth2client.client import AccessTokenRefreshError
from oauth2client.client import OAuth2WebServerFlow
import httplib2

scope = "https://www.googleapis.com/auth/fitness.activity.read"
flow = OAuth2WebServerFlow(client_id, client_secret, scope, redirect_url)
auth_uri = flow.step1_get_authorize_url()
print auth_uri

token = str(input("Enter token"))

cred = flow.step2_exchange(token)
storage = Storage("credentials_file")
storage.put(cred)

storage = Storage("credentials_file")
cred2 = storage.get()

http = httplib2.Http()
http = cred2.authorize(http)
service = build('fitness', 'v1',http=http)

但是,重复身份验证显然并不理想,因此我尝试将其转换为可以使用刷新令牌刷新访问令牌的代码。但是,运行此代码后, cred.refresh_token 和 cred2.refresh_token 都为 None。如何获取刷新令牌?

【问题讨论】:

    标签: python oauth-2.0 google-fit google-python-api


    【解决方案1】:

    更新,我知道为什么它是空的

    这一行:

    flow = OAuth2WebServerFlow(client_id, client_secret, scope, redirect_url)
    

    需要编辑如下:

    flow = OAuth2WebServerFlow(
                client_id,
                client_secret,
                scope,
                redirect_url,
                approval_prompt='force'
    )
    

    【讨论】:

      猜你喜欢
      • 2015-02-25
      • 1970-01-01
      • 2012-09-16
      • 2016-01-26
      • 2016-08-03
      • 2020-02-06
      • 1970-01-01
      • 2021-05-15
      • 2012-02-15
      相关资源
      最近更新 更多