是否有完整的基于Django的Google日历集成示例?我正在阅读Google's example page,但他们在底部的链接已过时。Google日历与Django的集成
由于Google的示例仅关注如何获取访问令牌,因此我特别喜欢刷新令牌。这是我到目前为止有:
@staff_member_required
def authorize_access(request):
return redirect(get_flow(request).step1_get_authorize_url())
@staff_member_required
def oauth2_callback(request):
credentials = get_flow(request).step2_exchange(request.GET['code'])
store = get_store()
store.put(credentials)
credentials.set_store(store)
return redirect('...')
def get_flow(request):
flow = client.flow_from_clientsecrets(os.path.join(CREDENTIAL_DIR, CLIENT_SECRET_FILE),
SCOPES,
redirect_uri='%s://%s/google-calendar/oauth2-callback/' % (request.META['wsgi.url_scheme'], request.META['HTTP_HOST'],))
flow.params['access_type'] = 'offline'
flow.params['approval_prompt'] = 'force'
return flow
def get_store():
return oauth2client.file.Storage(os.path.join(CREDENTIAL_DIR, ACCESS_TOKEN_FILE))
def has_valid_api_credentials():
credentials = get_store().get()
return credentials is not None
def build_service():
credentials = get_store().get()
if not credentials:
return None
elif credentials.access_token_expired:
http = credentials.refresh(httplib2.Http())
http = get_store().get().authorize(http)
else:
http = credentials.authorize(httplib2.Http())
service = discovery.build('calendar', 'v3', http=http)
return service
def create_events(rental_request):
service = build_service()
event = service.events().insert(...).execute()
非常感谢!现在,谷歌默认使用JSON密钥格式,所以你的代码可以从'credentials = ServiceAccountCredentials.from_p12_keyfile(service_account_email = service_account_email, filename = CLIENT_SECRET_FILE,scopes = SCOPES)'改为'credentials = ServiceAccountCredentials.from_json_keyfile_name(filename =,范围= SCOPES)' –