Of course. Here is a detailed, step-by-step guide to creating a Google Cloud project, enabling the Google Drive API, and downloading the necessary credentials.json file.
Overview
To allow your Python script to access your Google Drive, you need to register it as an "application" with Google. This process involves:
- Creating a Project: A container for your application's settings and APIs.
- Enabling the API: Specifically turning on the Google Drive API for your project.
- Configuring a Consent Screen: This is the pop-up window that will ask for your permission when you run the script for the first time.
- Creating Credentials: Generating a unique ID and a secret file (
credentials.json) that your script will use to identify itself to Google.
Let's go through each step.
Step 1: Go to the Google Cloud Console and Create a Project
-
Open your web browser and go to the Google Cloud Console.
-
Log in with the Google account that has access to the file you want to read from Google Drive.
-
If it's your first time, you may need to agree to the Terms of Service.
-
At the top of the page, click the project selector dropdown (it might say "Select a project").
-
In the dialog that appears, click the "NEW PROJECT" button.
-
Give your project a descriptive name, like Random Term Picker or My Drive Script. The Project ID will be automatically generated.
-
Click "CREATE".
Step 2: Enable the Google Drive API
-
Make sure your new project is selected in the project dropdown at the top of the page.
-
In the main search bar at the top of the console, type "Google Drive API" and press Enter.
-
Click on the "Google Drive API" result from the list.
-
You will be taken to the API's dashboard. Click the big blue "ENABLE" button. Wait a few moments for it to be enabled.
Step 3: Configure the OAuth Consent Screen
This step is crucial. It sets up the permission screen that you will see when you run the script.
-
After enabling the API, you should see a yellow warning banner prompting you to create credentials. Click "CREATE CREDENTIALS". If you don't see this, use the left-hand navigation menu and go to APIs & Services > OAuth consent screen.
-
For "User Type", select "External". This is the standard choice for personal projects.
-
Click "CREATE".
-
On the next page ("Edit app registration"), fill in the required fields:
- App name: Give your app a name (e.g., "My Random Term Script").
- User support email: Select your email address from the dropdown.
- Developer contact information: Enter your email address again at the bottom.
-
Click "SAVE AND CONTINUE".
-
On the "Scopes" page, you don't need to add anything. Just click "SAVE AND CONTINUE".
-
On the "Test users" page, you must add yourself as a test user.
- Click "+ ADD USERS".
- Enter the same Google email address you are logged in with.
- Click "ADD".
-
Click "SAVE AND CONTINUE".
-
You'll see a summary page. You can now proceed to create the credentials.
Step 4: Create and Download the Credentials
-
In the left-hand navigation menu, click on "Credentials".
-
At the top of the page, click "+ CREATE CREDENTIALS" and select "OAuth client ID".
-
For "Application type", select "Desktop app" from the dropdown menu. This is the correct type for a Python script running on your local machine.
-
Give the client ID a name (the default "Desktop client 1" is fine).
-
Click "CREATE".
-
A pop-up will appear showing your "Client ID" and "Client Secret". More importantly, there will be a "DOWNLOAD JSON" button. Click it.
-
Save the downloaded file. Rename it to credentials.json.
-
Place this credentials.json file in the same folder where your Python script is located.
You are now finished! You have successfully created a project, enabled the API, and obtained the credentials.json file needed for your script to authenticate with Google.