我正在尝试编写一个Django应用程序,用于在特定的Google日历中创建事件.到目前为止,我已经成功了.只有一点问题:
我不知道如何使用google python客户端获取刷新令牌.
结果是,在我的令牌过期后,应用程序无法运行,我必须创建一个新令牌.如果我理解文档正确,那就是刷新令牌的来源.
访问令牌的生命周期有限,在某些情况下,应用程序需要在单个访问令牌的生命周期之外访问Google API.在这种情况下,您的应用程序可以获得所谓的刷新令牌.刷新令牌允许您的应用程序获取新的访问令牌.
Google文档(请参阅"基本步骤",第4节)
import gflags
import httplib2
from apiclient.discovery import build
from oauth2client.file import Storage
from oauth2client.client import OAuth2WebServerFlow
from oauth2client.tools import run
FLAGS = gflags.FLAGS
FLOW = OAuth2WebServerFlow(
client_id=GOOGLE_API_CLIENT_ID,
client_secret=GOOGLE_API_CLIENT_SECRET,
scope=GOOGLE_API_CALENDAR_SCOPE,
user_agent=GOOGLE_API_USER_AGENT)
storage = Storage(GOOGLE_API_CREDENTIAL_PATH)
credentials = storage.get()
if credentials is None or credentials.invalid == True:
credentials = run(FLOW, storage)
http = httplib2.Http()
http = credentials.authorize(http)
service = build(serviceName='calendar', version='v3', http=http,
developerKey=GOOGLE_API_DEVELOPER_KEY)
event = {
[... Dictionary with all the necessary …Run Code Online (Sandbox Code Playgroud)