Comments (6)
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.
Already have the implementation for service accounts? I kind of need this. If so, how can i setup it?
from pydrive2.
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.
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.
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.
Nevermind, this was human error. The above code works well!
from pydrive2.
Related Issues (20)
- A simple way of initializing a pydrive.GoogleDrive object fails when pydrive is replaced by pydrive2 HOT 1
- Cant copy the file... using service account HOT 3
- Fail to authenticate with pydrive2 to google drive HOT 4
- Access blocked: This application request is invalid HOT 1
- Is there a Google Drive file sharing feature? HOT 3
- Unable to list files with service account auth for GDriveFilesystem HOT 3
- How do I write to specific tab? HOT 1
- fetch a specific folder id by folder name HOT 4
- Missing 1.17.1 release? HOT 1
- auth: LocalWebserverAuth: fail if in non-interactive mode
- ssl.SSLEOFError: EOF occurred in violation of protocol (_ssl.c:2427) HOT 2
- Review and fix issues with find
- Create Succesfull Authentication using Google OAuth client ID for a remote server accessible via SSH in a local machine HOT 1
- AssertionError on using fsspec with service account HOT 1
- [Q]Set Disable Download HOT 1
- Would you be interested to add type hints?
- pyopenssl is a dependency, but it does not seem to be used
- httplib2 is a direct dependency but is not added as one
- SaveCredentialsFile() does not store anything
- CommandLineAuth Generate Wrong Link HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pydrive2.