Python 访问令牌无效时的Google日历API
我正在尝试在谷歌日历中获取特定日期范围内的事件。我正在做以下工作:Python 访问令牌无效时的Google日历API,python,google-calendar-api,Python,Google Calendar Api,我正在尝试在谷歌日历中获取特定日期范围内的事件。我正在做以下工作: credentials = AccessTokenCredentials.from_json(token_json) http = httplib2.Http() if credentials.access_token_expired: credentials.refresh(http) else: http = credentials.authorize(http
credentials = AccessTokenCredentials.from_json(token_json)
http = httplib2.Http()
if credentials.access_token_expired:
credentials.refresh(http)
else:
http = credentials.authorize(http)
service = build(serviceName='calendar', version='v3', http=http)
events = service.events().list(calendarId='primary', pageToken=None,
timeMin=rfc3339(start_dt),
timeMax=rfc3339(end_dt))
events = events.execute()
由于某种原因,当我到达最后一行时,我得到以下错误:
AccessTokenCredentialsError: The access_token is expired or invalid and can't be refreshed.
当我使用断点时,它在if语句中从不说access\u token\u expired
。这个用例怎么总是失败?我在刷新代币方面有什么地方做错了吗
关于这一点,我阅读了以下内容:
尝试测试
凭据。无效的
我有一个小程序,用来确保存储的凭证对于gmail单元测试总是新鲜的。它可能/可能不适合你的情况
from oauth2client.client import OAuth2WebServerFlow
from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage
from oauth2client import tools
# the stored credentials file to use for these tests
TEST_CREDENTIALS_FILE = 'testjoe.storage'
def get_credentials():
"""Utility routine to returns credentials for testing use"""
# Location of the credentials storage file
storage = Storage(TEST_CREDENTIALS_FILE)
# Start the OAuth flow to retrieve credentials
flow = flow_from_clientsecrets('client_secret_native.json', scope='https://www.googleapis.com/auth/gmail.modify')
# Try to retrieve credentials from storage or run the flow to generate them
credentials = storage.get()
if credentials is None or credentials.invalid:
flags = tools.argparser.parse_args(args=[])
credentials = tools.run_flow(flow, storage, flags)
return credentials
你是怎么修好的?我正在使用相同的方法,无法刷新令牌。。。我将非常感谢任何指导。我正在将AccessTokenCredentials与已获得的令牌(手动用户同意)一起使用。我有两个令牌,但我无法刷新令牌!我的问题是我没有正确存储令牌的数据。我将凭据保存到我的数据库中。to_json()
。您能修复它吗?如果“是”,那么怎么做?