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
Open the Google Cloud Console
Select or create a project where you want to enable the Google Analytics API.
In the left-hand menu, navigate to APIs & Services > Library.
In the search bar, type “Google Analytics Data API” and select it from the search results.
Click Enable to activate the API for your project.
Obtaining OAuth 2.0 Credentials
In the Google Cloud Console, go to APIs & Services > Credentials.
Click on Create Credentials and choose OAuth client ID.
Select the application type as Web application.
Specify the name of your application.
In the Authorized redirect URIs field, enter the following URI:
https://developers.google.com/oauthplayground
Click Create.
Download the credentials file (JSON) containing your client_id and client_secret.
Go to APIs & Services > Oauth consent screen.
Select External for the user type.
Click Create.
Configure the OAuth consent screen by providing the the following information:
Application name
User support email
Developer email
Click Save And Continue and use the default settings for the remaining screens.
On the OAuth consent screen, click Publish App.
Using OAuth 2.0 Playground to Obtain a Refresh Token
Open the OAuth 2.0 Playground.
Click on the gear icon in the top-right corner to open the settings.
Check the option Use your own OAuth credentials and paste the client_id and client_secret from your downloaded credentials file.
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.
Click Authorize APIs. You will be redirected to a Google sign-in page to grant access.
After authorization, you will be redirected back to the playground. In the Step 2 section, click Exchange authorization code for tokens.
You will receive an access_token and a refresh_token. Copy and save the refresh_token for future use.