How to Use Google Search Console API with Python

Updated on: by Heysho

This guide will walk you through using the Google Search Console API with Python to analyze your search performance data. We'll cover the complete process in these simple steps:

  1. Creating a project in Google Cloud
  2. Enabling the Search Console API and obtaining authentication keys
  3. Setting up authentication in Google Colab

Let's dive in!

STEP 1. Create a Project in Google Cloud

First, access the Google Cloud Platform and create a new project.

Google Cloud Platform New Project Creation Screen
Create a new project in Google Cloud Platform

STEP 2. Enable the Search Console API

After creating your project, you'll need to enable the Search Console API and set up authentication credentials.

Google Cloud API Library Screen
Search for "Search Console API" in the Google Cloud API library
Search Console API Search Results
Select the Search Console API from the results
Search Console API Details Screen
Click "Enable" to activate the API
API Activation Confirmation Screen
Verify that the API has been successfully enabled
Navigation to Credentials Creation Screen
Click "Create credentials" to proceed to the next step

STEP 3. Create an OAuth Consent Screen

Next, you'll need to configure the OAuth consent screen to set up authentication for your application.

OAuth Consent Screen Settings
Start configuring the OAuth consent screen
User Type Selection Screen
Select "External" for the user type
Application Information Input Screen
Enter your application name and user support email
Scope Settings Screen
You can proceed with the default scope settings
Test User Addition Screen
Add your Google account as a test user
Settings Confirmation Screen
Review your settings and complete the setup
OAuth Consent Screen Creation Complete
OAuth consent screen setup is now complete

STEP 4. Create Credentials

Now create OAuth 2.0 credentials to obtain the Client ID and Client Secret needed for API authentication.

Authentication Credentials Creation Start Screen
Navigate to the credentials creation page
Credential Type Selection Screen
Select "OAuth 2.0 Client ID"
Application Type Selection Screen
Choose "Desktop app" for the application type
Client Name Input Screen
Enter a name for your client and click create
Authentication Credentials Acquisition Complete Screen
Your Client ID and Client Secret have been generated - make sure to save these securely

STEP 5. Load in Google Colab

# Install required python packages
!pip install oauth2client
!pip install google-api-python-client
!pip install httplib2
# Import required packages
from oauth2client.client import OAuth2WebServerFlow
from googleapiclient.discovery import build
import httplib2

CLIENT_ID = "★Paste your Client ID from Google Cloud here★"
CLIENT_SECRET = "★Paste your Client Secret from Google Cloud here★"

# Define OAuth scopes with read only access
OAUTH_SCOPE = 'https://www.googleapis.com/auth/webmasters.readonly'
# OAUTH_SCOPE = 'https://www.googleapis.com/auth/webmasters'

# Redirect URI to open Authorization Code Window in Browser
REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob'

# Build URL that generates Authorization Code
flow = OAuth2WebServerFlow(CLIENT_ID, CLIENT_SECRET, OAUTH_SCOPE, REDIRECT_URI)
authorize_url = flow.step1_get_authorize_url()

# Print Authorization URL
print("Go to the following link in your browser: " + authorize_url)

STEP 6. Authenticate Google Colab and Google Cloud Accounts

Access the authentication URL and copy the Authorization code that appears.

Google Authentication Screen
Access the authorization URL and log in with your Google account
Authorization Code Display Screen
Copy the authorization code displayed on this screen

STEP 7. Verify if the Search Console API is Loaded

# Exchange authorization code for credentials
code = "★Paste your authorization code here★"
credentials = flow.step2_exchange(code)

# Create an authorized http object
http = httplib2.Http()
http = credentials.authorize(http)

# Build the service object
webmasters_service = build('webmasters', 'v3', http=http)

# Get list of verified sites
site_list = webmasters_service.sites().list().execute()

# Print the list of sites
for site in site_list['siteEntry']:
    print(site['siteUrl'])

STEP 8. Extract Daily Data

# Define the site URL you want to analyze
site_url = 'https://your-website.com/'

# Define the query parameters
request = {
    'startDate': '2023-01-01',
    'endDate': '2023-01-31',
    'dimensions': ['date', 'query'],
    'rowLimit': 25000
}

# Execute the request
response = webmasters_service.searchanalytics().query(siteUrl=site_url, body=request).execute()

# Print the results
for row in response['rows']:
    print('Date: {}, Query: {}, Clicks: {}, Impressions: {}, CTR: {}, Position: {}'.format(
        row['keys'][0], row['keys'][1], row['clicks'], row['impressions'], row['ctr'], row['position']))

Conclusion

This guide covers the basic setup for accessing the Google Search Console API with Python. In future updates, we'll explore more advanced data analysis techniques. I hope you found this tutorial helpful!