How to Extract Verified Properties using Google Search Console API and Python

Share this post

This post is part of the Guide on Google Search Console API

In this post, I will show you how to check verified properties in Google Search Console using Python and the API.

This module will be useful when you want to run Google Search Console extraction on all your properties without manually listing it.

It will return something like this:

[
    'https://example.com',
    'https://example2.com',
    'https://example3.com'
]

Get Credentials

First, you are going to need to get a client_secrets.json file with your credentials. Check out how you can get Google Search Console OAuth Credentials.

Import OAuth Module

Then, you will need to add the oauth.py file that we have created in “Authorise Requests to GSC API Using OAuth 2.0” blog post, in the same folder as this script.

from oauth import authorize_creds, execute_request

Get Properties From Google Search Console

The get_property_list() function uses the webmasters_service authorized credential to get a list of verified URLs from Google Search Console.

def get_property_list(webmasters_service):
    '''
    Get a list of validated properties from GSC
    '''
    site_list = webmasters_service.sites().list().execute()

    # Filter for verified websites
    verified_sites_urls = [s['siteUrl'] for s in site_list['siteEntry']
                        if s['permissionLevel'] != 'siteUnverifiedUser'
                            and s['siteUrl'][:4] == 'http']
    return verified_sites_urls

Run the Function

To run the function, authorize the credentials first and then run the function we created above.

if __name__ == '__main__':
    creds = 'client_secrets.json'
    webmasters_service = authorize_creds(creds) 
    verified_sites_urls = get_property_list(webmasters_service)

The if name equals main line checks whether you are running the module or importing it. If you are importing it, authorize_creds() will not run.

Full Code

This code is available on Github.

#!/usr/bin/env python
'''
Get a list of validated properties in GSC.

@author:    Jean-Christophe Chouinard. 
@role:      Sr. SEO Specialist at SEEK.com.au
@website:   jcchouinard.com
@LinkedIn:  linkedin.com/in/jeanchristophechouinard/ 
@Twitter:   twitter.com/@ChouinardJC

Get API Keys
jcchouinard.com/how-to-get-google-search-console-api-keys/

How to format your request
jcchouinard.com/what-is-google-search-console-api/
'''

from oauth import authorize_creds, execute_request

def get_property_list(webmasters_service):
    '''
    Get a list of validated properties from GSC
    '''
    site_list = webmasters_service.sites().list().execute()

    # Filter for verified websites
    verified_sites_urls = [s['siteUrl'] for s in site_list['siteEntry']
                        if s['permissionLevel'] != 'siteUnverifiedUser'
                            and s['siteUrl'][:4] == 'http']
    return verified_sites_urls

if __name__ == '__main__':
    creds = 'client_secrets.json'
    webmasters_service = authorize_creds(creds) 
    verified_sites_urls = get_property_list(webmasters_service)

Using this module, you were able to get a list of validated properties from Google Search Console.