-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoogleApi.py
More file actions
33 lines (31 loc) · 1.38 KB
/
googleApi.py
File metadata and controls
33 lines (31 loc) · 1.38 KB
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
#!/usr/bin/python3.10
# -*- coding: utf-8 -*-
from os.path import exists
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from google.auth.exceptions import RefreshError
def setService (appli, scopes):
""" appli = drive ou calendar
scopes est une liste d'url de googleapis
"""
fileToken = 'token-' + appli + '.json'
fileCreds = 'credentials-' + appli + '.json'
creds = None
if not exists (fileCreds) and not exists (fileToken):
print ("impossible de récupérer les authentifications, aucun fichier d'authentification n'existe")
return None
elif exists (fileToken): creds = Credentials.from_authorized_user_file (fileToken, scopes)
if exists (fileCreds) and (not creds or not creds.valid):
flow = InstalledAppFlow.from_client_secrets_file (fileCreds, scopes)
creds = flow.run_local_server (port=0)
with open (fileToken, 'w') as token: token.write (creds.to_json())
if not creds or not creds.valid:
try:
creds.refresh (Request())
with open (fileToken, 'w') as token: token.write (creds.to_json())
except RefreshError: print ('impossible de récupérer les authentifications')
# récupérer le service
service = build (appli, 'v3', credentials=creds, client_options={ 'quota_project_id': appli + '-deborah' })
return service