Google 캘린더 API 및 Python을 사용한 CRUD
15341 단어 googlecalendarcrudpythonnelsoncode
requirements.txt
google-api-python-client
google-auth-httplib2
google-auth-oauthlib
pip install -r requirements.txt
get_service.py
from os import path
import pickle
from google.auth.transport.requests import Request
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
# IF YOU MODIFY THE SCOPE DELETE THE TOKEN.TXT FILE
SCOPES = ['https://www.googleapis.com/auth/calendar.events',
'https://www.googleapis.com/auth/calendar']
# THE TOKEN.TXT FILE STORES THE USER'S UPGRADE AND ACCESS TOKENS
# creds.json -> Credentials in Oauth0
def get_crendetials_google():
# OPEN THE BROWSER TO AUTHORIZE
flow = InstalledAppFlow.from_client_secrets_file("creds.json", SCOPES)
creds = flow.run_local_server(port=0)
# SAVE CREDENTIALS
pickle.dump(creds, open("token.txt", "wb"))
return creds
def get_calendar_service():
creds = None
if path.exists("token.txt"):
creds = pickle.load(open("token.txt", "rb"))
# IF IT EXPIRED WE REFRESH THE CREDENTIALS
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
creds = get_crendetials_google()
service = build("calendar", "v3", credentials=creds)
return service
creds.json
{
"web": {
"client_id": "",
"project_id": "",
"auth_uri": "",
"token_uri": "",
"auth_provider_x509_cert_url": "",
"client_secret": "",
"redirect_uris": ["http://localhost/"]
}
}
템플릿 JSON
{
"summary": "Important event!",
"location": "virtual event (Slack)",
"description": "this is a description",
"start": {
"dateTime": "2021-12-10T10:00:00",
"timeZone": "America/El_Salvador"
},
"end": {
"dateTime": "2021-12-10T11:00:00",
"timeZone": "America/El_Salvador"
},
"attendees": [{ "email": "[email protected]" }],
"reminders": {
"useDefault": false,
"overrides": [
{ "method": "email", "minutes": 30 },
{ "method": "popup", "minutes": 10 }
]
}
}
CRUD 구글 캘린더
calendar.py
from functions.get_service import get_calendar_service
from responses.response_json import response_json
service = get_calendar_service()
def create_event(template: dict):
try:
response = service.events().insert(calendarId="primary", body=template).execute()
return response
except Exception as e:
return e.message
def get_event(eventId: str):
try:
response = service.events().get(calendarId="primary", eventId=eventId).execute()
return response
except Exception as e:
return e.message
def delete_event(eventId: str):
try:
response = service.events().delete(calendarId="primary", eventId=eventId).execute()
return response
except Exception as e:
return e.message
def update_event(eventId: str, template: dict):
try:
response = service.events().update(calendarId='primary',
eventId=eventId, body=template).execute()
return response
except Exception as e:
return e.message
Reference
이 문제에 관하여(Google 캘린더 API 및 Python을 사용한 CRUD), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/nelsoncode/crud-with-google-calendar-api-python-3jbc텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)