python connect to google caleder

Solutions on MaxInterview for python connect to google caleder by the best coders in the world

showing results for - "python connect to google caleder"
Jimmy
06 Apr 2018
1import datetime
2import pickle
3import os.path
4from googleapiclient.discovery import build
5from google_auth_oauthlib.flow import InstalledAppFlow
6from google.auth.transport.requests import Request
7
8# If modifying these scopes, delete the file token.pickle.
9SCOPES = ['https://www.googleapis.com/auth/calendar']
10
11CREDENTIALS_FILE = 'path_to_file/credentials.json'
12
13def get_calendar_service():
14   creds = None
15   # The file token.pickle stores the user's access and refresh tokens, and is
16   # created automatically when the authorization flow completes for the first
17   # time.
18   if os.path.exists('token.pickle'):
19       with open('token.pickle', 'rb') as token:
20           creds = pickle.load(token)
21   # If there are no (valid) credentials available, let the user log in.
22   if not creds or not creds.valid:
23       if creds and creds.expired and creds.refresh_token:
24           creds.refresh(Request())
25       else:
26           flow = InstalledAppFlow.from_client_secrets_file(
27               CREDENTIALS_FILE, SCOPES)
28           creds = flow.run_local_server(port=0)
29
30       # Save the credentials for the next run
31       with open('token.pickle', 'wb') as token:
32           pickle.dump(creds, token)
33
34   service = build('calendar', 'v3', credentials=creds)
35   return service