-
Notifications
You must be signed in to change notification settings - Fork 7
/
ct_gdrive_oauth2.py
executable file
·91 lines (71 loc) · 2.63 KB
/
ct_gdrive_oauth2.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
#!/usr/bin/python
#
# Google Drive Lustre/HSM lhsmtool_cmd copytool companion
# OAuth2 Credentials helper script
#
# Written by Stephane Thiell <[email protected]>
# Stanford Research Computing
#
# Copyright 2016
# The Board of Trustees of the Leland Stanford Junior University
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Foobar. If not, see <http://www.gnu.org/licenses/>.
"""
ct_gdrive_oauth2.py
Perform OAuth2 Flow to store credentials for ct_gdrive.py
"""
from __future__ import print_function
import argparse
import os
import sys
from apiclient import discovery
import oauth2client
from oauth2client import client
from oauth2client import tools
# Google Drive API application name
APPLICATION_NAME = 'ct_gdrive'
# drive.file is per-file access to files created or opened by the app
SCOPES = 'https://www.googleapis.com/auth/drive.file'
# on-disk credentials filename
OAUTH2_STORAGE_CREDS_FILENAME = 'ct_gdrive_creds.json'
def get_parser():
"""ct_gdrive command line options"""
parser = argparse.ArgumentParser(add_help=False)
parser.add_argument("--client-secret", required=True)
# Credentials directory
parser.add_argument("--creds-dir", required=True)
return argparse.ArgumentParser(parents=[tools.argparser, parser])
args = get_parser().parse_args()
# oauth2 client: do not run a local web server
args.noauth_local_webserver = True
def new_credentials():
"""Perform OAuth2 flow to obtain the new credentials.
Return:
Credentials, the obtained credential.
"""
credential_path = os.path.join(args.creds_dir, 'ct_gdrive_creds.json')
store = oauth2client.file.Storage(credential_path)
credentials = store.get()
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(args.client_secret, SCOPES)
flow.user_agent = APPLICATION_NAME
credentials = tools.run_flow(flow, store, args)
print('Storing credentials to ' + credential_path)
return credentials
def main():
"""main ct_gdrive_oauth2 entry point"""
creds = new_credentials()
return 0
if __name__ == '__main__':
sys.exit(main())