Enabling Google Analytics API and Obtaining OAuth Credentials

This guide provides step-by-step instructions for enabling the Google Analytics API and obtaining OAuth credentials and a refresh token using the OAuth 2.0 Playground.

Warning

The configuration outlined here is intended solely for demonstration purposes.

For production environments, consult your security team to ensure the appropriate configuration is applied.

Enabling the Google Analytics API

  1. Open the Google Cloud Console

  2. Select or create a project where you want to enable the Google Analytics API.

  3. In the left-hand menu, navigate to APIs & Services > Library.

  4. In the search bar, type “Google Analytics Data API” and select it from the search results.

  5. Click Enable to activate the API for your project.

Obtaining OAuth 2.0 Credentials

  1. In the Google Cloud Console, go to APIs & Services > Credentials.

  2. Click on Create Credentials and choose OAuth client ID.

    ../_images/1.png
  3. Select the application type as Web application.

  4. Specify the name of your application.

  5. In the Authorized redirect URIs field, enter the following URI:

    https://developers.google.com/oauthplayground
    
  6. Click Create.

  7. Download the credentials file (JSON) containing your client_id and client_secret.

  8. Go to APIs & Services > Oauth consent screen.

  9. Select External for the user type.

  10. Click Create.

  11. Configure the OAuth consent screen by providing the the following information:

    • Application name

    • User support email

    • Developer email

  12. Click Save And Continue and use the default settings for the remaining screens.

  13. On the OAuth consent screen, click Publish App.

    ../_images/2.png

Using OAuth 2.0 Playground to Obtain a Refresh Token

  1. Open the OAuth 2.0 Playground.

  2. Click on the gear icon in the top-right corner to open the settings.

  3. Check the option Use your own OAuth credentials and paste the client_id and client_secret from your downloaded credentials file.

    ../_images/31.png
  4. In the Step 1 section, select the Google Analytics Data API v1beta > https://www.googleapis.com/auth/analytics scope from the list of available APIs.

    ../_images/4.png
  5. Click Authorize APIs. You will be redirected to a Google sign-in page to grant access.

  6. After authorization, you will be redirected back to the playground. In the Step 2 section, click Exchange authorization code for tokens.

  7. You will receive an access_token and a refresh_token. Copy and save the refresh_token for future use.