Code Monkey home page Code Monkey logo

Comments (6)

tmotyl avatar tmotyl commented on May 25, 2024 11

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.

from pydrive2.

trolliama avatar trolliama commented on May 25, 2024 1

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

from pydrive2.

shcheklein avatar shcheklein commented on May 25, 2024 1

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.

from pydrive2.

shcheklein avatar shcheklein commented on May 25, 2024

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.

from pydrive2.

astrochun avatar astrochun commented on May 25, 2024

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

from pydrive2.

astrochun avatar astrochun commented on May 25, 2024

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

from pydrive2.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.