
Setting Up Google Search Console (GSC) Inspection API for Automated Monitoring
In this article, we’ll walk you through the process of setting up the Google Search Console (GSC) Inspection API for automated monitoring. The GSC Inspection API allows you to retrieve information about your website’s search engine ranking and visibility, enabling you to monitor and improve your online presence.
Prerequisites
Before getting started, ensure you have:
- A Google Search Console account set up for the website you want to monitor
- A Google Cloud Platform (GCP) project created
- The
gcloud
command-line tool installed on your machine
Step 1: Create a GSC Inspection API Key
To use the GSC Inspection API, you need to create an API key. Follow these steps:
Create a new API key in Google Cloud Console
- Log in to the Google Cloud Console.
- Navigate to the APIs & Services > Dashboard page.
- Click on Enable APIs and Services.
- Search for Search Console Inspection API and click on the result.
- Click on the Enable button.
Create a new API key
- In the left-hand menu, select APIs & Services > Credentials.
- Click on Create Credentials > OAuth client ID.
- Choose Other as the application type and enter a name for your API key (e.g.,
gsc-inspection-api
). - Create a new client ID.
Restrict the API key to the GSC Inspection API
- Go back to the APIs & Services > Credentials page.
- Click on the three vertical dots next to your newly created API key and select Edit.
- In the OAuth 2.0 client ID details section, add
https://www.googleapis.com/auth/search-console-inspection
as a Authorized redirect URI.
Step 2: Set up GSC Inspection API in Google Cloud Console
Create a new service account
- Go to the Google Cloud Console.
- Navigate to the APIs & Services > Dashboard page.
- Click on Enable APIs and Services.
- Search for Service accounts and click on the result.
- Click on Create Service Account.
Create a new service account key
- In the left-hand menu, select APIs & Services > Credentials.
- Click on Create Credentials > Service account key.
- Choose the email address of your newly created service account as the Service account.
- Select JSON as the Key type.
Grant access to the GSC Inspection API
- Go back to the APIs & Services > Credentials page.
- Click on the three vertical dots next to your service account key and select Edit.
- In the OAuth 2.0 client ID details section, add
https://www.googleapis.com/auth/search-console-inspection
as a Authorized redirect URI.
Step 3: Install and configure the GSC Inspection API library
Install the required libraries
- Run
pip install google-auth-oauthlib google-api-python-client
to install the required libraries. - You can also use a Python package manager like
poetry
orpipenv
.
Configure the GSC Inspection API library
- Create a new file (e.g.,
gsc_inspection_api_config.py
) and add the following code:
“`python
import os
Your Google Cloud Console project ID
PROJECT_ID = ‘your-project-id’
Your service account email address
SERVICE_ACCOUNT_EMAIL = ‘your-service-account-email’
The path to your service account key file
KEY_FILE_PATH = ‘path/to/your/service/account/key.json’
“`
2. Replace the placeholders with your actual values.
Step 4: Run the GSC Inspection API library
- You can now run the
gsc_inspection_api.py
script, which is a basic example of how to use the GSC Inspection API.
“`python
import os
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
The Google Search Console API key
API_KEY = ‘your-api-key’
The service account credentials
creds = None
if creds is None or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
os.path.join(os.getcwd(), ‘gsc_inspection_api_config.py’),
scopes=[‘https://www.googleapis.com/auth/search-console-inspection’])
creds = flow.run_local_server(port=0)
Build the Google Search Console API client
service = build(‘searchconsole’, ‘v1’, credentials=creds, developerKey=API_KEY)
``
service` object to make requests to the GSC Inspection API.
2. You can now use the
This concludes our step-by-step guide on setting up the Google Search Console (GSC) Inspection API for automated monitoring. The GCS Inspection API allows you to retrieve information about your website’s search engine ranking and visibility, enabling you to monitor and improve your online presence.