Tutorial: Setting Up Google OAuth 2.0 and Using the OAuth Playground with Node.js
This tutorial guides you through creating a Google Cloud project, setting up OAuth 2.0 credentials, and using the OAuth Playground to obtain a refresh token for Node.js integration.
Step 1: Create a Google Cloud Project
Go to the Google Cloud Console.
Click Select a Project > New Project.
Enter a Project Name and choose your Organization (if applicable).
Click Create and wait for the project to be created.
Step 2: Set Up OAuth 2.0 Credentials
Navigate to the APIs & Services section on the left sidebar and select Credentials.
Click Create Credentials > OAuth Client ID.
If prompted, configure the OAuth consent screen:
- Set the User Type to External.
- Add a suitable App Name.
- Set the Publishing Status to Testing.
- Add Test Users by entering their email addresses.
- Leave Scopes blank for now.
- Save the configuration.
Return to Credentials and continue creating an OAuth client:
- Select Web Application as the application type.
- Enter a suitable name.
- In Authorized Redirect URIs, add:
1
https://developers.google.com/oauthplayground
- Click Create.
Copy the Client ID and Client Secret from the generated credentials.
Step 3: Authorize APIs in the OAuth Playground
Visit the OAuth Playground.
In the top-right corner, click the Settings icon.
- Provide the Client ID and Client Secret from the previous step.
- Check the box for āUse your own OAuth credentials.ā
- Save the settings.
On the left side, under Select & authorize APIs, expand the Google Drive API category.
- Select the scope
https://www.googleapis.com/auth/drive
. - Click Authorize APIs.
- Select the scope
Complete the authorization by signing in with a test user account you added earlier.
Click Exchange authorization code for tokens to generate an access token and refresh token.
Copy the Refresh Token displayed.
Tutorial: How to Get the File ID from Google Drive
The File ID is a unique identifier for each file stored in Google Drive. It is required when using Google Drive APIs to manage or manipulate files programmatically.
Using the Google Drive Web Interface
Open Google Drive Visit Google Drive and log in with your Google account.
Locate Your File Navigate to the file for which you want to retrieve the File ID.
Right-Click and Select āGet Linkā
- Right-click on the file and choose āGet Linkā.
- Ensure the link-sharing settings are configured appropriately (e.g., anyone with the link can view).
Copy the File ID
- The shared link will look something like this:
1
https://drive.google.com/file/d/FILE_ID/view?usp=sharing
- The portion between /d/ and /view is the File ID. For example, if the link is:
1
https://drive.google.com/file/d/1MMPSPbuXezEOucdYvbgIsZtiYcdYa7qT4eBdv_gWm3M/view?usp=sharing
The File ID is:
1
1MMPSPbuXezEOucdYvbgIsZtiYcdYa7qT4eBdv_gWm3M
- The shared link will look something like this:
Tutorial: Use Node.js in transferring ownership
Install Dependencies
|
|
Node.js Code Example
|
|
Instructions for Use
Replace the placeholders (your-client-id, your-client-secret, your-refresh-token, your-file-id, new-owner-email@example.com) with your actual data.
Follow the tutorial to set up your Google OAuth 2.0 credentials and obtain the refresh token.
Use this code in your Node.js project to transfer file ownership securely.