In this post, I will show you how to check verified properties in Google Search Console using Python and the GSC Sites API.
This module will be useful when you want to run Google Search Console extraction on all your properties without manually listing them.
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
This is the content of the oauth.py
file.
import argparse
import httplib2
import requests
from collections import defaultdict
from dateutil import relativedelta
from googleapiclient.discovery import build
from oauth2client import client
from oauth2client import file
from oauth2client import tools
def authorize_creds(creds,authorizedcreds='authorizedcreds.dat'):
'''
Authorize credentials using OAuth2.
'''
print('Authorizing Creds')
# Variable parameter that controls the set of resources that the access token permits.
SCOPES = ['https://www.googleapis.com/auth/webmasters']
# Path to client_secrets.json file
CLIENT_SECRETS_PATH = creds
# Create a parser to be able to open browser for Authorization
parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
parents=[tools.argparser])
flags = parser.parse_args([])
# Creates an authorization flow from a clientsecrets file.
# Will raise InvalidClientSecretsError for unknown types of Flows.
flow = client.flow_from_clientsecrets(
CLIENT_SECRETS_PATH, scope = SCOPES,
message = tools.message_if_missing(CLIENT_SECRETS_PATH))
# Prepare credentials and authorize HTTP
# If they exist, get them from the storage object
# credentials will get written back to the 'authorizedcreds.dat' file.
storage = file.Storage(authorizedcreds)
credentials = storage.get()
# If authenticated credentials don't exist, open Browser to authenticate
if credentials is None or credentials.invalid:
credentials = tools.run_flow(flow, storage, flags) # Add the valid creds to a variable
# Take the credentials and authorize them using httplib2
http = httplib2.Http() # Creates an HTTP client object to make the http request
http = credentials.authorize(http=http) # Sign each request from the HTTP client with the OAuth 2.0 access token
webmasters_service = build('searchconsole', 'v1', http=http)#build('webmasters', 'v3', http=http) # Construct a Resource to interact with the API using the Authorized HTTP Client.
print('Auth Successful')
return webmasters_service
# Create Function to execute your API Request
def execute_request(service, property_uri, request):
return service.searchanalytics().query(siteUrl=property_uri, body=request).execute()
if __name__ == '__main__':
creds = 'client_secrets.json'
webmasters_service = authorize_creds(creds)
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
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 using an API.
SEO Strategist at Tripadvisor, ex- Seek (Melbourne, Australia). Specialized in technical SEO. Writer in Python, Information Retrieval, SEO and machine learning. Guest author at SearchEngineJournal, SearchEngineLand and OnCrawl.