-
Notifications
You must be signed in to change notification settings - Fork 71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
docs: add Service account example #21
Comments
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. |
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. |
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 ---------------------------------------------------------------------------
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 Other possible examples are in the thread below. |
Her is an example for this https://docs.iterative.ai/PyDrive2/oauth/#authentication-with-a-service-account . Where:
It means that service account credentials could be passed via
dict
, via file, viajson
string.Other possible examples are in the thread below.
Related, but should be improved - googlearchive/PyDrive#157 . See auth tests for an example how we use service account by setting up a proper yaml file.
The text was updated successfully, but these errors were encountered: