I've been trying to use a service account to download files from Google Drive via the API and some examples I found in the documentation, and for a while it worked fine. A few days ago though, I started getting this error:
{
"error": {
"errors": [
{
"domain": "usageLimits",
"reason": "dailyLimitExceededUnreg",
"message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.",
"extendedHelp": "https://code.google.com/apis/console"
}
],
"code": 403,
"message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup."
}
}
Well, actually I get a different 403 in my console (only files with binary content can be downloaded), but when I click on the link in that error, it leads to the above JSON.
EDIT: The above bolded part was the real issue, and then when I clicked the link it was unauthorized. My mistake!
This is the code I'm running, basically pulled from their docs:
def get_drive_service(key_file):
sa_creds = service_account.Credentials.from_service_account_file(key_file)
scoped_creds = sa_creds.with_scopes(SCOPES)
drive_service = build('drive', 'v3', credentials=scoped_creds)
return drive_service
def download_report(drive_service, id):
request = drive_service.files().get_media(fileId=id)
print(request.to_json())
fh = io.BytesIO()
downloader = MediaIoBaseDownload(fh, request, chunksize=1024*1024)
done = False
while done is False:
status, done = downloader.next_chunk()
print("Download %d%%." % int(status.progress() * 100))
return fh
drive_service = get_drive_service(<key_file>)
buffer = download_report(drive_service, <file_id>)
I found some similar questions on SO, and tried everything that seemed relevant, but nothing has worked. The API is enabled, and the quotas are not an issue, when I use files().list().execute() I get exactly the list of files I would expect, and as I said the code used to work fine.
I'm flummoxed. Can anyone please help me understand what I'm missing here?