googlepythonapiclient刷新令牌是nu

2024-05-23 17:25:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试在googlepythonapi客户端中使用刷新令牌,以避免每次都让用户验证应用程序。在

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

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_令牌cred2.refresh_令牌均为空。如何获得刷新令牌?在


Tags: 代码用户fromimportclienttokenauthhttp