
PageSize=5, fields="nextPageToken, files(id, name, mimeType, size, parents, modifiedTime)").execute() Prints the names and ids of the first 5 files the user has access to. """Shows basic usage of the Drive v3 API. Going to the main function, let's define a function that lists files in our drive: def main(): After that, it'll initiate the Google Drive API service and return it. It basically looks for token.pickle file to authenticate with your Google account, if it didn't find it, it'll use credentials.json to prompt you for authentication in your browser.

We've imported the necessary modules, the above function was grabbed from Google Drive quickstart page.

Return build('drive', 'v3', credentials=creds) With open('token.pickle', 'wb') as token: If creds and creds.expired and creds.refresh_token:įlow = om_client_secrets_file( # If there are no (valid) credentials available, let the user log in. With open('token.pickle', 'rb') as token: # created automatically when the authorization flow completes for the first # The file token.pickle stores the user's access and refresh tokens, and is # If modifying these scopes, delete the file token.pickle. List Files and Directoriesīefore we do anything, we need to authenticate our code to our Google account, the below function does that: import pickleįrom googleapiclient.discovery import buildįrom google_auth_oauthlib.flow import InstalledAppFlowįrom import Request

After that, you'll see another window appear saying you're all set:ĭownload your credentials by clicking the "Download Client Configuration" button and then hit "Done".įinally, you need to put credentials.json that is just downloaded into your working directories (i.e where you execute the upcoming Python scripts).

First, you need to have a Google account with Google Drive enabled, head to this page and click the "Enable the Drive API" button as shown below:Ī new window will pop up, choose your type of application, I'm gonna stick with "Desktop app", then hit the "Create" button. To get started, let's install the required libraries for this tutorial: pip3 install google-api-python-client google-auth-httplib2 google-auth-oauthlib tabulate requests tqdm Enable the Drive APIĮnabling Google Drive API is very similar to other Google APIs such as Gmail API, YouTube API, or Google Search Engine API. In this tutorial, you will learn how you can list your Google drive files, search over them, download stored files, and even upload local files into your drive programmatically using Python. Google Drive enables you to store your files in the cloud in which you can access anytime and everywhere in the world.
