【发布时间】:2021-12-01 02:05:34
【问题描述】:
我想为我的 Django 应用程序使用 Google Calendar API。 我已按照此处的说明进行操作:https://karenapp.io/articles/how-to-automate-google-calendar-with-python-using-the-calendar-api/
我还在 Google API 中添加了重定向 uri - 似乎浏览器试图在服务器端打开(就像在本地一样,但我无法操作它,因为服务器端的浏览器没有显示向上)。在终端中,我确实看到“请访问此 URL 以授权此应用程序:https://accounts.google.com/..."
关于我能做什么的任何想法?
views.py 中的代码:
import datetime
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
SCOPES = ['https://www.googleapis.com/auth/calendar']
CREDENTIALS_FILE = 'path_to_file/credentials.json'
def get_calendar_service():
creds = None
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
CREDENTIALS_FILE, SCOPES)
creds = flow.run_local_server(port=0)
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('calendar', 'v3', credentials=creds)
return service
【问题讨论】:
标签: python django heroku google-calendar-api google-api-python-client