-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgcloud_services.py
56 lines (46 loc) · 2.02 KB
/
gcloud_services.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
46
47
48
49
50
51
52
53
54
55
56
import os.path
from google.oauth2.credentials import Credentials
from google.auth.transport.requests import Request
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from dotenv import load_dotenv
load_dotenv()
def get_services():
token = None
SCOPES = ['https://www.googleapis.com/auth/calendar',
'https://www.googleapis.com/auth/gmail.send',
'https://www.googleapis.com/auth/userinfo.profile',
'https://www.googleapis.com/auth/userinfo.email',
'openid']
creds = None
if os.path.exists('token.json'):
# if token:
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
# creds = Credentials.from_authorized_user_file(token, SCOPES)
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_config(
{
"installed": {
"client_id": os.getenv("CLIENT_ID"),
"project_id": os.getenv("PROJECT_ID"),
"auth_uri": os.getenv("AUTH_URI"),
"token_uri": os.getenv("TOKEN_URI"),
"auth_provider_x509_cert_url": os.getenv("AUTH_PROVIDER_X509_CERT_URL"),
"client_secret": os.getenv("CLIENT_SECRET"),
"redirect_uris": [
os.getenv("REDIRECT_URI")
]
}
},
SCOPES)
creds = flow.run_local_server(port=0)
with open('token.json', 'w') as token:
token.write(creds.to_json())
# token = creds.to_json()
user_service = build('oauth2', 'v2', credentials=creds)
calendar_service = build('calendar', 'v3', credentials=creds)
mail_service = build('gmail', 'v1', credentials=creds)
return user_service, calendar_service, mail_service