-
Notifications
You must be signed in to change notification settings - Fork 245
Open
Description
Google Calendar API
Using the google-api-python-client Package
Reference
Google Calendar API Docs:
- https://developers.google.com/calendar/overview
- https://developers.google.com/calendar/v3/reference/calendars
Installation
pip install google-api-python-client
pip install oauth2client==4.1.3NOTE: apparently oauth2client is going away, and in order for this code to work, you have to use python 3.7 (not a more recent version).
So ideally these docs will be updated to use thegoogle-authpackage instead. Contributions welcome!
Setup
After creating a new Google Cloud project via the Google Cloud APIs console, and enabling the Google Calendar API...
Create service account credentials for this project, and download the resulting JSON key file into the root directory of this repo, for example named "google-credentials.json".
If you want to use the environment variable approach, from the root directory of this repo, set the credentials as an environment variable:
export GOOGLE_API_CREDENTIALS="$(< google-credentials.json)"
echo $GOOGLE_API_CREDENTIALSUsage
# adapted from / reference:
# ... https://developers.google.com/calendar/quickstart/python
# ... https://github.com/googleworkspace/python-samples/blob/master/calendar/quickstart/quickstart.py
# ... https://bitbucket.org/kingmray/django-google-calendar/src/master/calendar_api/calendar_api.py
import os
import json
from pprint import pprint
#from datetime import datetime
#import datetime
#import pytz
from googleapiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials
GOOGLE_API_CREDENTIALS = os.getenv("GOOGLE_API_CREDENTIALS")
CREDS_JSON = json.loads(GOOGLE_API_CREDENTIALS)
#CREDS_FILEPATH = os.path.join(os.path.dirname(__file__), "auth", "google-credentials.json")
CREDS_FILEPATH = os.path.join(os.path.dirname(__file__), "..", "google-credentials.json")
# see: # https://developers.google.com/identity/protocols/oauth2/scopes#calendar
AUTH_SCOPES = [
"https://www.googleapis.com/auth/calendar", # read/write access to Calendars
#"https://www.googleapis.com/auth/calendar.readonly", # read-only access to Calendars
#"https://www.googleapis.com/auth/calendar.events", # read/write access to Events
#"https://www.googleapis.com/auth/calendar.events.readonly", # read-only access to Events
#"https://www.googleapis.com/auth/calendar.settings.readonly", # read-only access to Settings
#"https://www.googleapis.com/auth/calendar.addons.execute", # run as a Calendar add-on
#"https://www.googleapis.com/auth/gmail.send",
#"https://www.googleapis.com/auth/admin.directory.resource.calendar"
]
if __name__ == "__main__":
#credentials = ServiceAccountCredentials._from_parsed_json_keyfile(CREDS_JSON, AUTH_SCOPES)
credentials = ServiceAccountCredentials.from_json_keyfile_name(CREDS_FILEPATH, AUTH_SCOPES)
client = build("calendar", "v3", credentials=credentials)
print("------------")
print("CALENDARS:")
calendars = client.calendarList().list().execute()
pprint(calendars)
print("------------")
if any(calendars):
calendar_id = input("PLEASE CHOOSE A CALENDAR ID: ") or calendars["items"][0]["id"]
# https://developers.google.com/calendar/v3/reference/calendars/get
calendar = client.calendars().get(calendarId=calendar_id).execute() #> dict
pprint(calendar)
else:
calendar_info = {
"summary": "Example Calendar",
"description": "Used for development and testing purposes",
"timeZone": "US/Eastern",
}
print(calendar_info)
# https://developers.google.com/calendar/v3/reference/calendars/insert#python
# https://developers.google.com/calendar/v3/reference/calendars#resource
calendar = client.calendars().insert(body=calendar_info).execute() #> dict
breakpoint()Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels