I’m having trouble with my Python script that downloads files from Google Drive using service account credentials.
I have a service account key file called auth_data.json
in my project folder. My code looks like this:
from googleapiclient.discovery import build
from googleapiclient.http import MediaIoBaseDownload
from google.oauth2 import service_account
import io
auth_info = {"auth_data.json"} # service account json data goes here
creds = service_account.Credentials.from_service_account_info(auth_info)
gdrive_api = build('drive', 'v3', credentials=creds)
document_id = 'my_file_ID'
api_request = gdrive_api.files().get_media(fileId=document_id)
file_handler = io.FileIO('downloaded_archive.tar.gz', 'wb')
download_handler = MediaIoBaseDownload(file_handler, api_request)
finished = False
while not finished:
progress, finished = download_handler.next_chunk()
print(f"Progress: {int(progress.progress() * 100)}%")
But I keep getting this error message:
Traceback (most recent call last):
File "gdrive_downloader.py", line 9, in <module>
creds = service_account.Credentials.from_service_account_info(auth_info)
File "/usr/local/lib/python3.10/site-packages/google/oauth2/service_account.py", line 243, in from_service_account_info
signer = _service_account_info.from_dict(
File "/usr/local/lib/python3.10/site-packages/google/auth/_service_account_info.py", line 47, in from_dict
missing = keys_needed.difference(data.keys())
AttributeError: 'set' object has no attribute 'keys'
I tried creating a new service account key but still get the same error. What am I doing wrong here?