我正在创建管理信息中心,并希望显示Google Analytics中的图表。为此,我已按照发现服务器端授权here的步骤进行操作。直到本例结束,一切都很好。问题是访问令牌在一小时后过期,需要重新生成。
我还没有找到有关如何刷新此令牌的更多信息。有没有办法自动刷新此令牌?
import json
from oauth2client.client import SignedJwtAssertionCredentials
# The scope for the OAuth2 request.
SCOPE = 'https://www.googleapis.com/auth/analytics.readonly'
# The location of the key file with the key data.
KEY_FILEPATH = 'path/to/json-key.json'
# Load the key file's private data.
with open(KEY_FILEPATH) as key_file:
_key_data = json.load(key_file)
# Construct a credentials objects from the key data and OAuth2 scope.
_credentials = SignedJwtAssertionCredentials(
_key_data['client_email'], _key_data['private_key'], SCOPE)
# Defines a method to get an access token from the credentials object.
# The access token is automatically refreshed if it has expired.
def get_access_token():
return _credentials.get_access_token().access_token
print get_access_token()