Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

docs: add Service account example #21

Closed
shcheklein opened this issue Mar 25, 2020 · 6 comments
Closed

docs: add Service account example #21

shcheklein opened this issue Mar 25, 2020 · 6 comments
Labels
documentation Improvements or additions to documentation good first issue Good for newcomers hacktoberfest help wanted Extra attention is needed

Comments

@shcheklein
Copy link
Member

shcheklein commented Mar 25, 2020

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.


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.

@shcheklein shcheklein added documentation Improvements or additions to documentation good first issue Good for newcomers help wanted Extra attention is needed labels Mar 25, 2020
@trolliama
Copy link

Already have the implementation for service accounts? I kind of need this. If so, how can i setup it?

@tmotyl
Copy link
Contributor

tmotyl commented Sep 28, 2021

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.

tmotyl added a commit to tmotyl/PyDrive2 that referenced this issue Sep 29, 2021

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
Fixes: iterative#21
@shcheklein
Copy link
Member Author

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.

@shcheklein shcheklein changed the title add Service account example docs: add Service account example Jul 24, 2022
@astrochun
Copy link

astrochun commented Aug 5, 2022

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')

@astrochun
Copy link

Nevermind, this was human error. The above code works well!

@shcheklein
Copy link
Member Author

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.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
documentation Improvements or additions to documentation good first issue Good for newcomers hacktoberfest help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants