I am trying to make a program that downloads files stored in Google Drive, I have looked for how to do it in many places, but I cannot make my program work correctly.
This is my code:
from __future__ import print_function
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly']
def main():
"""Shows basic usage of the Drive v3 API.
Prints the names and ids of the first 10 files the user has access to.
"""
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('drive', 'v3', credentials=creds)
# file_id is an example
file_id = '0BwwA4oUTeiV1UVNwOHItT0xfa2M'
request = drive_service.files().get_media(fileId=file_id)
fh = io.BytesIO()
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
status, done = downloader.next_chunk()
print ("Download %d%%.") % int(status.progress() * 100)
If I run the program I get this: file_id = '0BwwA4oUTeiV1UVNwOHItT0xfa2M' ^ IndentationError: unindent does not match any outer indentation level
I have also tried Pydrive, but it doesnt work for me.
I don´t know where is the error.
EDIT
I solved the error thanks to @caldweln, but now another error has appeared. When I run the program, the CMD doesn't show any errors. But my Google Drive file has not been downloaded
The lines below and starting with 'file_id=....' are indented with 2 spaces from the left. However the code above it are indented with 4 spaces. If these lines of code are to be within the 'main' function, they need to be indented like the rest of the code... 4 spaces at a time.
This is a common error when copying and pasting code. If you plan to further extend the code, either modify the copied code to match or change your style to suit. Note development environments like PyCharm and VSCode have settings to configure the default indentation.