How to Get Google Drive API Client ID, Client Secret, and Refresh Token?


If you want to integrate Google Drive into your website and need a Client ID, Client Secret, and Refresh Token, then in this tutorial we will learn how to get all three step by step.

Whether you are building an automatic website backup system, a WordPress backup plugin, a Python automation script, or any web application that uploads files directly to Google Drive in the background so your files stay safe, you need three main credentials from the Google Drive API: Client ID, Client Secret, and Refresh Token. You can easily generate all of these from the Google Cloud Console by following a few steps.

Generating a Client ID and Client Secret in the Google Cloud Console is fairly simple, but if you are a beginner and have never created Google API credentials before, getting the Refresh Token can be confusing—this is the exact step where most people get stuck.

In this guide, we will go through the complete process of generating your Google Drive API Client ID, Client Secret, and Refresh Token step by step.

Step 1: Create a Google Cloud Project and Enable Google Drive API

  1. Open the Google Cloud Console in your browser and log in with your Google (Gmail) account.
  2. Click the project dropdown at the top left and select New Project.
  3. Give your project a clear name (for example, Website Backup App) and click Create.
  4. Once the project is created, select it from the notification popup or the top project dropdown.
  5. From the left sidebar menu, go to APIs & Services → Library.
  6. Type Google Drive API in the search box, click on the result, and press the blue ENABLE button.

Step 2: Set Up the OAuth Consent Screen (To Avoid 7-Day Expiry)

After enabling the API, you need to tell Google what kind of users will access your app.

  1. From the left menu, go to APIs & Services → OAuth consent screen (in the newer Google Cloud dashboard, this appears under Google Auth Platform → Overview / Audience).
  2. Click Get Started and fill in the basic details:
    • App name: Enter your app or website name (for example, My Drive Backup).
    • User support email: Select your Gmail address from the dropdown.
    • Audience: Select External.
    • Contact Information: Enter your Gmail address and click Create / Save.
  3. Most Important Setting (For a Lifetime Token):
    • On the Audience (or OAuth consent screen) page, check the Publishing status. By default, it is set to Testing.
    • In Testing mode, your Refresh Token expires after just 7 days. To make it permanent, click the "PUBLISH APP" button and click Confirm.
    • (Note: If this app is only for your own Google Drive, you do not need to submit it to Google for verification.)

Note: If you see Error 403: access_denied while logging in, read our guide: Fix: Access blocked: backup App has not completed the Google verification process

Step 3: Generate OAuth 2.0 Client ID and Client Secret

  1. Go to Clients (or APIs & Services → Credentials) from the left menu.
  2. Click + Create Client (or + CREATE CREDENTIALS → OAuth client ID) at the top.
  3. For Application type, select Web application.
  4. Enter any name in the Name field (for example, Drive Backup Web Client).
  5. Scroll down to Authorized redirect URIs, click + ADD URI, and paste the exact URL below without any extra spaces or trailing slash:
    https://developers.google.com/oauthplayground
  6. Now click the Create button.
  7. A popup will appear showing your Client ID and Client Secret. Copy both and save them in a safe file (or click Download JSON).

Step 4: Get the Refresh Token Using Google OAuth 2.0 Playground

This is the most important part of the entire setup. If you skip points 2 and 3 below, your Refresh Token will expire in just 24 hours.

  1. Open Google OAuth 2.0 Playground in a new tab.
  2. Click the Settings (⚙️ Gear Icon) in the top-right corner of the screen.
  3. At the bottom of the settings panel, check the box labeled "Use your own OAuth credentials".
  4. Two new input fields will appear:
    • OAuth Client ID: Paste the Client ID you copied earlier.
    • OAuth Client secret: Paste the Client Secret you copied earlier.
    • Click Close.
  5. On the left side under Step 1: Select & authorize APIs, scroll down and expand Drive API v3.
  6. Select the scope below so your script can upload and manage files:
    https://www.googleapis.com/auth/drive

    (If you want the app to only access files that it creates itself, you can choose https://www.googleapis.com/auth/drive.file instead.)

  7. Click the blue Authorize APIs button.
  8. Select the Gmail account whose Google Drive you want to upload files to.
  9. You will see a "Google hasn't verified this app" warning screen. Click Advanced at the bottom left, click Go to [Your App Name] (unsafe), and then press Continue on the next screen.
  10. You will be redirected back to OAuth Playground at Step 2: Exchange authorization code for tokens, where the Authorization code box will already be filled in.
  11. Click the "Exchange authorization code for tokens" button.

Your Refresh token and Access token will appear immediately in the response box below. Copy the Refresh token and keep it safe, because Google does not show it again for security reasons.

Bonus Step: How to Get Your Google Drive Folder ID

If you want your PHP or Python script to upload backup files inside a specific folder (such as Website Backups) instead of the main root directory:

  1. Open Google Drive and create a new folder.
  2. Double-click the folder to open it.
  3. Look at your browser's address bar—the URL will look something like this:
    https://drive.google.com/drive/folders/1A2b3C4d5E6f7G8h9I0jKlMnOpQrStUvW
  4. Everything after /folders/ (1A2b3C4d5E6f7G8h9I0jKlMnOpQrStUvW) is your Folder ID.

Quick Test: Test Your Refresh Token with PHP cURL

Before adding these credentials to your main project, you can run this short PHP cURL script to verify in 10 seconds that all three keys are working properly:

<?php
$clientId     = 'YOUR_CLIENT_ID';
$clientSecret = 'YOUR_CLIENT_SECRET';
$refreshToken = 'YOUR_REFRESH_TOKEN';

$ch = curl_init('https://oauth2.googleapis.com/token');
curl_setopt_array($ch, [
    CURLOPT_POST           => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POSTFIELDS     => http_build_query([
        'client_id'     => $clientId,
        'client_secret' => $clientSecret,
        'refresh_token' => $refreshToken,
        'grant_type'    => 'refresh_token'
    ])
]);

$response = curl_exec($ch);
curl_close($ch);

echo '<pre>';
print_r(json_decode($response, true));
echo '</pre>';
?>

If the output returns a new access_token along with expires_in: 3599, your Google Drive API setup is 100% working.

3 Most Common Mistakes Developers Make

  1. Error 400: redirect_uri_mismatch: This happens when you forget to add https://developers.google.com/oauthplayground in Step 3, or accidentally add an extra / at the end. Make sure the URI matches character for character.
  2. Not checking "Use your own OAuth credentials" in OAuth Playground: If you don't enter your own Client ID and Client Secret under the Gear Icon (⚙️), Google generates a token using its default demo credentials. That token won't work in your PHP script and gets deleted after 24 hours.
  3. Leaving the app in "Testing" mode: A Refresh Token created in Testing mode starts throwing an invalid_grant error after 7 days. Always click PUBLISH APP to move it to In Production status.

Next Step (Related Tutorial)

Now that all your Google Drive API keys are ready, you can use them to set up 100% free automatic backups for your website:
Auto-Backup PHP Website & MySQL Database Directly to Google Drive (Free Cron Job)

Frequently Asked Questions (FAQs)

Q1: Is using the Google Drive API completely free?

Yes. Using the Google Drive API is 100% free, and you do not need to add a credit card or billing account in Google Cloud. You get to use the 15 GB of free storage included with your Google account.

Q2: What is the difference between an Access Token and a Refresh Token?

An Access Token is a temporary key that only works for 1 hour (3600 seconds). A Refresh Token is a long-term key that allows your PHP script to automatically generate a fresh Access Token in the background every time it makes an API call.

Q3: Does a Refresh Token ever expire?

If your Google Cloud app is in In Production mode, the Refresh Token does not expire unless: (1) you change your Google account password, (2) you revoke the app's access from your Google Account Security settings, or (3) the token goes completely unused for 6 consecutive months.

Q4: Why did OAuth Playground only give me an Access Token and no Refresh Token?

This usually happens if you previously authorized the same app and Google didn't issue a new Refresh Token. To fix this, go to your Google Account Permissions, remove access for that app, and run Step 4 again in OAuth Playground.


Please Share