π How to Set Up Google Drive Picker with OAuth 2.0 and API Key
Google Drive Picker is a powerful JavaScript library that allows users to easily select files from their Google Drive within your web application. To use it, you’ll need to configure credentials in Google Cloud Console:
-
OAuth 2.0 Client ID (for authentication)
-
API Key (for Google Drive and Picker APIs)
In this guide, we’ll walk through the process step by step and show you how to integrate Google Drive Picker in your app.
π Demo Project: drivepicker.pythonanywhere.com
π Source Code: GitHub – Google Drive File Picker
π οΈ Step 1: Create a Google Cloud Project
-
Go to Google Cloud Console.
-
Sign in with your Google account.
-
At the top left, click the project dropdown → New Project.
-
Give your project a name (e.g., Drive Picker Demo) → click Create.
π Step 2: Enable Required APIs
-
Inside your project, navigate to:
APIs & Services > Library. -
Enable the following APIs:
-
Google Drive API
-
Google Picker API
-
π Step 3: Configure OAuth Consent Screen
Before creating OAuth credentials, configure your app’s consent screen:
-
In the left menu, go to APIs & Services > OAuth consent screen.
-
Select External if you’re not using Google Workspace.
-
Fill out required details:
-
App name (e.g., Drive Picker App)
-
User support email
-
Developer contact information
-
-
Add scopes:
https://www.googleapis.com/auth/drive.readonly -
Add your test users (at least your own Google account).
-
Save your configuration.
π§© Step 4: Create OAuth 2.0 Client ID
-
Go to APIs & Services > Credentials.
-
Click Create Credentials → OAuth Client ID.
-
Choose Web application.
-
Enter a name (e.g., Drive Picker Client).
-
Under Authorized JavaScript origins, add your domain:
-
For development:
http://localhost:5500 -
For production: your deployed domain
-
-
Leave Authorized redirect URIs blank (Picker doesn’t need it).
-
Click Create → copy your Client ID.
π Save this as GOOGLE_CLIENT_ID in your .env file.
π Step 5: Create an API Key
-
Still in Credentials, click Create Credentials → API Key.
-
Copy the generated key → this is your GOOGLE_API_KEY.
-
(Highly recommended) Secure your API key:
-
API restrictions: Restrict to Google Drive API and Google Picker API.
-
Application restrictions: Restrict by HTTP referrers.
-
Example for dev:
http://localhost:5500/* -
Example for prod:
https://yourdomain.com/*
-
-
π Save this as GOOGLE_API_KEY in your .env file.
βοΈ Step 6: Plug Into Your Project
Now that you have your Client ID and API Key, you can integrate Google Drive Picker.
π― Final Notes
β
You now have a working Google Drive Picker integration with proper authentication.
β
Store your API Key and Client ID securely (use environment variables in production).
β
Always restrict your API keys to reduce security risks.
π Try it live here: drivepicker.pythonanywhere.com
π Fork the source: GitHub – Google Drive File Picker
β¨ With this setup, you can let users pick files directly from their Google Drive inside your web app.
0 Comments