-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcal_setup.py
45 lines (36 loc) · 1.48 KB
/
cal_setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
'''
Before Starting using Google OAuth SDK for Python, install
pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib
first.
'''
'''
Import the following libraries
'''
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
# You can change the scope of the application, make sure to delete token.pickle file first
SCOPES = ['https://www.googleapis.com/auth/calendar']
CREDENTIALS_FILE = 'credentials.json' # Give path to your credentials.json file
def get_calendar_service():
cred = None
'''
The file token.pickle stores the user's access and refresh tokens, and is created automatically when
the authorization flow completes for the first time. In other words when the user give access to this
channel
'''
if os.path.exists('token.pickle'):
with open('token.pickle','rb') as token:
cred = pickle.load(token)
if not cred or not cred.valid:
if cred and cred.expired and cred.refresh_token:
cred.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(CREDENTIALS_FILE, SCOPES)
cred = flow.run_local_server(port=0)
with open('token.pickle', 'wb') as token:
pickle.dump(cred, token)
service = build('calendar','v3',credentials=cred)
return service