PyDrive2
PyDrive2 copied to clipboard
docs: add Service account example
Related, but should be improved - https://github.com/gsuitedevs/PyDrive/pull/157 . See auth tests for an example how we use service account by setting up a proper yaml file.
Already have the implementation for service accounts? I kind of need this. If so, how can i setup it?
here is an example which works for me
from pydrive2.auth import GoogleAuth
from pydrive2.drive import GoogleDrive
from oauth2client.service_account import ServiceAccountCredentials
scope = ["https://www.googleapis.com/auth/drive"]
gauth = GoogleAuth()
gauth.auth_method = 'service'
gauth.credentials = ServiceAccountCredentials.from_json_keyfile_name('client_secrets.json', scope)
drive = GoogleDrive(gauth)
about = drive.GetAbout()
print('Current user name:{}'.format(about['name']))
print('Root folder ID:{}'.format(about['rootFolderId']))
print('Total quota (bytes):{}'.format(about['quotaBytesTotal']))
print('Used quota (bytes):{}'.format(about['quotaBytesUsed']))
file_list = drive.ListFile().GetList()
for file1 in file_list:
print('title: %s, id: %s' % (file1['title'], file1['id']))
Where client_secrets.json is a file downloaded from google cloud console when creating a service account. Also note that you have to share documents with service account email for them to ba available.
There is a PR that @tmotyl prepared that needs just some minor improvements and is almost ready to be merged. We would appreciate any help on this.
here is an example which works for me
from pydrive2.auth import GoogleAuth from pydrive2.drive import GoogleDrive from oauth2client.service_account import ServiceAccountCredentials scope = ["https://www.googleapis.com/auth/drive"] gauth = GoogleAuth() gauth.auth_method = 'service' gauth.credentials = ServiceAccountCredentials.from_json_keyfile_name('client_secrets.json', scope) drive = GoogleDrive(gauth)
I tried this but came across an error. Perhaps I have the wrong client_secrets.json format or perhaps something changed in the past year or so.
And yes I tried, "service_account" for gauth.auth_method
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Input In [8], in <cell line: 8>()
6 gauth = GoogleAuth()
7 gauth.auth_method = 'service'
----> 8 gauth.credentials = ServiceAccountCredentials.from_json_keyfile_name('client_secrets.json', scope)
9 drive = GoogleDrive(gauth)
File ~/codes/****/venv/lib/python3.10/site-packages/oauth2client/service_account.py:221, in ServiceAccountCredentials.from_json_keyfile_name(cls, filename, scopes, token_uri, revoke_uri)
219 with open(filename, 'r') as file_obj:
220 client_credentials = json.load(file_obj)
--> 221 return cls._from_parsed_json_keyfile(client_credentials, scopes,
222 token_uri=token_uri,
223 revoke_uri=revoke_uri)
File ~/codes/***/venv/lib/python3.10/site-packages/oauth2client/service_account.py:171, in ServiceAccountCredentials._from_parsed_json_keyfile(cls, keyfile_dict, scopes, token_uri, revoke_uri)
169 creds_type = keyfile_dict.get('type')
170 if creds_type != client.SERVICE_ACCOUNT:
--> 171 raise ValueError('Unexpected credentials type', creds_type,
172 'Expected', client.SERVICE_ACCOUNT)
174 service_account_email = keyfile_dict['client_email']
175 private_key_pkcs8_pem = keyfile_dict['private_key']
ValueError: ('Unexpected credentials type', None, 'Expected', 'service_account')
Nevermind, this was human error. The above code works well!
Closing this, since we have an example now in the docs:
Her is an example for this https://docs.iterative.ai/PyDrive2/oauth/#authentication-with-a-service-account . Where:
service_config:
client_user_email: {{str}}
client_json_file_path: {{str}}
client_json_dict: {{dict}}
client_json: {{str}}
It means that service account credentials could be passed via dict
, via file, via json
string.
Other possible examples are in the thread below.