Skip to content

Commit 3ef3e9e

Browse files
committed
Change default name of token file for Service account scripts
- Also added missing script file Signed-off-by: lzzy12 <[email protected]>
1 parent 47dd3c2 commit 3ef3e9e

File tree

2 files changed

+79
-2
lines changed

2 files changed

+79
-2
lines changed

add_to_team_drive.py

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
from __future__ import print_function
2+
from google.oauth2.service_account import Credentials
3+
import googleapiclient.discovery, json, progress.bar, glob, sys, argparse, time
4+
from google_auth_oauthlib.flow import InstalledAppFlow
5+
from google.auth.transport.requests import Request
6+
import os, pickle
7+
8+
stt = time.time()
9+
10+
parse = argparse.ArgumentParser(
11+
description='A tool to add service accounts to a shared drive from a folder containing credential files.')
12+
parse.add_argument('--path', '-p', default='accounts',
13+
help='Specify an alternative path to the service accounts folder.')
14+
parse.add_argument('--credentials', '-c', default='./credentials.json',
15+
help='Specify the relative path for the credentials file.')
16+
parse.add_argument('--yes', '-y', default=False, action='store_true', help='Skips the sanity prompt.')
17+
parsereq = parse.add_argument_group('required arguments')
18+
parsereq.add_argument('--drive-id', '-d', help='The ID of the Shared Drive.', required=True)
19+
20+
args = parse.parse_args()
21+
acc_dir = args.path
22+
did = args.drive_id
23+
credentials = glob.glob(args.credentials)
24+
25+
try:
26+
open(credentials[0], 'r')
27+
print('>> Found credentials.')
28+
except IndexError:
29+
print('>> No credentials found.')
30+
sys.exit(0)
31+
32+
if not args.yes:
33+
# input('Make sure the following client id is added to the shared drive as Manager:\n' + json.loads((open(
34+
# credentials[0],'r').read()))['installed']['client_id'])
35+
input('>> Make sure the **Google account** that has generated credentials.json\n is added into your Team Drive '
36+
'(shared drive) as Manager\n>> (Press any key to continue)')
37+
38+
creds = None
39+
if os.path.exists('token_sa.pickle'):
40+
with open('token_sa.pickle', 'rb') as token:
41+
creds = pickle.load(token)
42+
# If there are no (valid) credentials available, let the user log in.
43+
if not creds or not creds.valid:
44+
if creds and creds.expired and creds.refresh_token:
45+
creds.refresh(Request())
46+
else:
47+
flow = InstalledAppFlow.from_client_secrets_file(credentials[0], scopes=[
48+
'https://www.googleapis.com/auth/admin.directory.group',
49+
'https://www.googleapis.com/auth/admin.directory.group.member'
50+
])
51+
# creds = flow.run_local_server(port=0)
52+
creds = flow.run_console()
53+
# Save the credentials for the next run
54+
with open('token_sa.pickle', 'wb') as token:
55+
pickle.dump(creds, token)
56+
57+
drive = googleapiclient.discovery.build("drive", "v3", credentials=creds)
58+
batch = drive.new_batch_http_request()
59+
60+
aa = glob.glob('%s/*.json' % acc_dir)
61+
pbar = progress.bar.Bar("Readying accounts", max=len(aa))
62+
for i in aa:
63+
ce = json.loads(open(i, 'r').read())['client_email']
64+
batch.add(drive.permissions().create(fileId=did, supportsAllDrives=True, body={
65+
"role": "fileOrganizer",
66+
"type": "user",
67+
"emailAddress": ce
68+
}))
69+
pbar.next()
70+
pbar.finish()
71+
print('Adding...')
72+
batch.execute()
73+
74+
print('Complete.')
75+
hours, rem = divmod((time.time() - stt), 3600)
76+
minutes, sec = divmod(rem, 60)
77+
print("Elapsed Time:\n{:0>2}:{:0>2}:{:05.2f}".format(int(hours), int(minutes), sec))

gen_sa_accounts.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ def _delete_sas(iam, project):
163163

164164
def serviceaccountfactory(
165165
credentials='credentials.json',
166-
token='token.pickle',
166+
token='token_sa.pickle',
167167
path=None,
168168
list_projects=False,
169169
list_sas=None,
@@ -282,7 +282,7 @@ def serviceaccountfactory(
282282
parse = ArgumentParser(description='A tool to create Google service accounts.')
283283
parse.add_argument('--path', '-p', default='accounts',
284284
help='Specify an alternate directory to output the credential files.')
285-
parse.add_argument('--token', default='token.pickle', help='Specify the pickle token file path.')
285+
parse.add_argument('--token', default='token_sa.pickle', help='Specify the pickle token file path.')
286286
parse.add_argument('--credentials', default='credentials.json', help='Specify the credentials file path.')
287287
parse.add_argument('--list-projects', default=False, action='store_true',
288288
help='List projects viewable by the user.')

0 commit comments

Comments
 (0)