I'm a GSuite Administrator with nearly 300 users and I'm migrating away from GSuite. I need to download all our user files they've created/uploaded.
I'm starting small with writing a Python script that will show me user files in a list and seems like I'm stuck with the overwhelming authorization issues.
- I've created a project in Google Console, and created a Service Account with private key (json based) and GSuite Domain-wide delegation checkbox ticked
- In my GSuite Admin panel I've added the newly created client ID and permission scope in Manage API access to these scopes:
https://www.googleapis.com/auth/drive,https://www.googleapis.com/auth/admin.datatransfer,https://www.googleapis.com/auth/admin.directory.user, https://www.googleapis.com/auth/admin.directory.group
So good according to the documentation (https://developers.google.com/admin-sdk/directory/v1/guides/delegation)
I'm creating a resource object from the ServiceAccountCredentials and building an object based off API name/version "drive" and "v3" respectively and trying to get files list according to Google's quickstart (https://developers.google.com/drive/api/v3/quickstart/python):
from googleapiclient.discovery import build from oauth2client.service_account import ServiceAccountCredentials SERVICE_ACCOUNT_EMAIL = "[email protected]" SERVICE_ACCOUNT_PKCS12 = "./service_key.json" def create_directory_service(user_email): credentials = ServiceAccountCredentials.from_json_keyfile_name(SERVICE_ACCOUNT_PKCS12, scopes=['https://www.googleapis.com/auth/drive']) credentials = credentials.create_delegated(user_email) return build('drive', 'v3', credentials=credentials) resource = create_directory_service('[email protected]') results = resource.files().list( pageSize=10, fields="nextPageToken, files(id, name)" ).execute() items = results.get('files', []) print(items)
It looks totally correct, but I get this error message:
Connected to pydev debugger (build 181.5087.37)
Traceback (most recent call last): File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py", line 1664, in main() File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py", line 1658, in main globals = debugger.run(setup['file'], None, None, is_module) File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py", line 1068, in run pydev_imports.execfile(file, globals, locals) # execute the script File "/Applications/PyCharm.app/Contents/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile exec(compile(contents+"\n", file, 'exec'), glob, loc) File "/Users/probe/Projects/drive_getter/src/dconnect.py", line 16, in pageSize=10, fields="nextPageToken, files(id, name)" File "/Users/probe/Projects/drive_getter/drive_getter/lib/python3.6/site-packages/googleapiclient/_helpers.py", line 130, in positional_wrapper return wrapped(*args, **kwargs) File "/Users/probe/Projects/drive_getter/drive_getter/lib/python3.6/site-packages/googleapiclient/http.py", line 835, in execute method=str(self.method), body=self.body, headers=self.headers) File "/Users/probe/Projects/drive_getter/drive_getter/lib/python3.6/site-packages/googleapiclient/http.py", line 162, in _retry_request resp, content = http.request(uri, method, *args, **kwargs) File "/Users/probe/Projects/drive_getter/drive_getter/lib/python3.6/site-packages/oauth2client/transport.py", line 159, in new_request credentials._refresh(orig_request_method) File "/Users/probe/Projects/drive_getter/drive_getter/lib/python3.6/site-packages/oauth2client/client.py", line 749, in _refresh self._do_refresh_request(http) File "/Users/probe/Projects/drive_getter/drive_getter/lib/python3.6/site-packages/oauth2client/client.py", line 819, in _do_refresh_request raise HttpAccessTokenRefreshError(error_msg, status=resp.status) oauth2client.client.HttpAccessTokenRefreshError: unauthorized_client: Client is unauthorized to retrieve access tokens using this method.
Any idea what was done wrong in the process? Again - My goal is to list and later download all user files from all GSuite users, so I was thinking of looping my user emails and applying the same logic to all of them until I get all the files downloaded.
Thanks for any cooperation!
fields="files(id, name)"
this much only? – Kishor Pawar