-
Notifications
You must be signed in to change notification settings - Fork 272
Authenticating Service Accounts #107
Comments
Same error here, I need to use service account because my server will upload files direct to my account, any news about this issue? |
i'm getting the same error too for a service account. |
Hi all, could you post the version of |
I've got 4.1.2 |
Has anyone had any success with this yet? |
Any more on this? |
It works for me as:
|
The code that @ghostku posted worked perfectly for me (fyi, I don't understand why, but |
@ghostku 's method works perfect, would be great to have it documented somewhere. Or to be able to have the json file be taken from the default or yaml config and not hardcoded. |
Using @ghostku 's method and the following example code from the projects documentation
results in the follow error for me
|
Never mind, I was using the wrong |
#107 (comment) |
Tried to use @ghostku code, but I'm getting no files (empty list) when running:
When using
|
@ghostku Same issue as @shlomiLan |
@tamilarasumk731 could you try the PyDrive2 fork? We've fixed a bunch of errors related to the services accounts and missing options to different API calls to work properly with shared drives/shared folders (usual reason for list to be empty, but not sure if that's the case in your case) |
I want to be able to read the credentials from an environment variable, what will be the best way to do that? def init_connection():
credentials_data = os.environ.get('GOOGLE_SERVICE_ACCOUNT_CREDENTIALS')
if not credentials_data:
raise KeyError('No credentials data, missing environment variable')
credentials_data = json.loads(credentials_data)
# Fix the 'private_key' escaping
credentials_data['private_key'] = credentials_data.get('private_key').encode().decode('unicode-escape')
from oauth2client.service_account import ServiceAccountCredentials
scope = ['https://www.googleapis.com/auth/drive']
credentials = ServiceAccountCredentials.from_json_keyfile_dict(credentials_data, scope)
return credentials
def list_files():
creds = init_connection()
drive_service = build('drive', 'v3', credentials=creds)
# Call the Drive v3 API
results1 = drive_service.files().list(
pageSize=10, fields="nextPageToken, files(id, name)").execute()
print(results1)
from pydrive2.auth import GoogleAuth
from pydrive2.drive import GoogleDrive
gauth = GoogleAuth()
gauth.credentials = creds
drive = GoogleDrive(gauth)
result2 = drive.ListFile({'q': "'root' in parents"}).GetList()
print(result2) In |
When using @ghostku's workaround, you should also set the auth method: gauth.auth_method = 'service' Otherwise if you do many operations over a long time your access token will expire, and PyDrive(2) will incorrectly try to refresh the access token via the |
I set gauth.auth_method = 'service', but after 1 hour of execution, the same exception still occurs. Do you know the reason? Or, is there any solution? I use service account's delegated credentials. Thank you very much. code show as below: scope = ['https://www.googleapis.com/auth/drive'] gauth = GoogleAuth() |
Hi,
I'm trying to build an application and hope to use service account credentials for uploading and downloading. I'm having some issues authenticating the service account by following PyDrive's documentation.
After creating my service account, I renamed the "secret" to 'client_secrets.json' and I encounter the following error message when I authenticate:
When I look at auth.py, I see the function defined as ServiceAuth().
I'm able to use google's stock api to achieve listfile:
How do I go about authenticating a service account via PyDrive?
The text was updated successfully, but these errors were encountered: