/
connection.py
44 lines (37 loc) · 1.7 KB
/
connection.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
import json
from aiogoogle.auth.creds import ServiceAccountCreds
# from google.auth.transport.requests import Request
# from google.oauth2.credentials import Credentials
# from google_auth_oauthlib.flow import InstalledAppFlow
class Connection:
def __init__(self, scopes):
self.service_account_key = json.load(open('service-account-creds.json'))
self.scopes = scopes
self.creds = None
def get_creds(self):
self.creds = ServiceAccountCreds(
scopes=self.scopes,
**self.service_account_key
)
return self.creds
# def __init__(self, scopes):
# self.scopes = scopes
# os.environ['OAUTHLIB_RELAX_TOKEN_SCOPE'] = '1'
# self.creds = None
# def make_connection(self):
# # The file token.json stores the user's access and refresh tokens, and is
# # created automatically when the authorization flow completes for the first
# # time.
# if os.path.exists('token.json'):
# self.creds = Credentials.from_authorized_user_file('token.json', self.scopes)
# # If there are no (valid) credentials available, let the user log in.
# if not self.creds or not self.creds.valid:
# if self.creds and self.creds.expired and self.creds.refresh_token:
# self.creds.refresh(Request())
# else:
# flow = InstalledAppFlow.from_client_secrets_file('credentials.json', self.scopes)
# self.creds = flow.run_local_server(port=0)
# # Save the credentials for the next run
# with open('token.json', 'w') as token:
# token.write(self.creds.to_json())
# return self.creds