-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_google_oauth_permissions.py
97 lines (72 loc) · 2.94 KB
/
get_google_oauth_permissions.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
"""
This file is intended to get the Google oAuth permissions,
in order to use the Google Sheet API.
This file must be run on local machine with access to
an browser window, since this command opens a new browser tab to
get the user consent from the Google user.
This is a one time operation to give those user permission to the
provided Google oAuth credentials (client-id and client-secret).
"""
from argparse import ArgumentParser
import json
import os
from google_auth_oauthlib.flow import InstalledAppFlow
from proversity_reports_script.get_settings import get_settings
# SCOPE for read, create, delete and update Google Spreadsheets data.
SCOPES = ['https://www.googleapis.com/auth/spreadsheets']
REQUIRED_CREDENTIALS = [
'token', 'refresh_token', 'token_uri', 'client_id', 'client_secret', 'scopes',
]
def main():
"""
Opens a new tab browser to get the user consent permission to
read and update his Google Sheet data.
"""
parser = ArgumentParser()
parser.add_argument('--config-file', '-c', help='Path to configuration file.', required=True)
args = parser.parse_args()
os.environ['CONFIGURATION_FILE_PATH'] = args.config_file
global_settings = get_settings(should_set_environment_settings=False)
google_oauth_credentials = global_settings.get('GOOGLE_OAUTH_CREDENTIALS', None)
if not google_oauth_credentials:
print('Google oAuth credentials were not provided.')
exit()
try:
flow = InstalledAppFlow.from_client_config(
google_oauth_credentials,
SCOPES
)
credentials = flow.run_local_server()
if not credentials:
raise GoogleApiCredentialsError('Credentials object is empty.')
store_credentials_as_dict('google-oauth-credentials.json', credentials)
except Exception as error:
raise error
def store_credentials_as_dict(file_name, credentials):
"""
Saves a file containing the dict object as json with the required information.
Args:
file_name: File name to store the credentials.
credentials: google.oauth2.credentials.Credentials Object.
Raises:
GoogleApiCredentialsError: When some of the required fields is missing from the credentials object.
"""
required_data = {}
for field in REQUIRED_CREDENTIALS:
credential_field = getattr(credentials, field, None)
if not credential_field:
error_message = 'Credential field is missing. {}'.format(field)
raise GoogleApiCredentialsError(error_message)
required_data.update({
field: credential_field
})
with open(file_name, 'w') as json_file:
json.dump(required_data, json_file, sort_keys=True)
class GoogleApiCredentialsError(Exception):
"""
Exception class raised when a Google oAuth credentials
is missing, empty, or there is other problem related to Google credentials object.
"""
pass
if __name__ == '__main__':
main()