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 ))
0 commit comments