Complete the steps described in the rest of this page, and you'll have a simple Search Console app that fetches a list of sites verified in your Search Console account and lists any sitemap files submitted.
To run a quickstart example you'll need:
- Access to the internet and a web browser, in order to authorize the sample app.
- A Google account with at least one website verified in Google Search Console.
- An environment to run programs in your selected language.
Step 1: Enable the Search Console API
To get started using Google Search Console API, you need to first use the setup tool, which guides you through creating a project in the Google API Console, enabling the API, and creating credentials.
- From the Credentials page, click Create credentials > OAuth client ID to create your OAuth 2.0 credentials or Create credentials > Service account key to create a service account.
- If you created an OAuth client ID, then select your application type.
- Fill in the form and click Create.
Take note of the Client ID as you'll need to add it to your code later.
Step 2: Install the Google Client Library
To install the Google API Python Client on a system, use either the
pip command or
easy_install --upgrade google-api-python-client
pip install --upgrade google-api-python-client
If you need to access the Google API Python Client from a Google App Engine project, follow the instructions here.
Step 3: Set up the sample
The next step is to copy and modify some sample code so that it can authenticate with the unique Client ID and Client Secret you created in the "Enable the Search Console API" step.
- After following the directions below to copy source code, replace
YOUR_CLIENT_IDwith the Client ID you generated in the "Enable the Google Search Console API" step.
YOUR_CLIENT_SECRETwith your Client Secret, also generated previously.
Copy the following source code to
#!/usr/bin/python import httplib2 from apiclient import errors from apiclient.discovery import build from oauth2client.client import OAuth2WebServerFlow # Copy your credentials from the console CLIENT_ID = 'YOUR_CLIENT_ID' CLIENT_SECRET = 'YOUR_CLIENT_SECRET' # Check https://developers.google.com/webmaster-tools/search-console-api-original/v3/ for all available scopes OAUTH_SCOPE = 'https://www.googleapis.com/auth/webmasters.readonly' # Redirect URI for installed apps REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob' # Run through the OAuth flow and retrieve credentials flow = OAuth2WebServerFlow(CLIENT_ID, CLIENT_SECRET, OAUTH_SCOPE, REDIRECT_URI) authorize_url = flow.step1_get_authorize_url() print 'Go to the following link in your browser: ' + authorize_url code = raw_input('Enter verification code: ').strip() credentials = flow.step2_exchange(code) # Create an httplib2.Http object and authorize it with our credentials http = httplib2.Http() http = credentials.authorize(http) webmasters_service = build('searchconsole', 'v1', http=http) # Retrieve list of properties in account 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'] # Print the URLs of all websites you are verified for. for site_url in verified_sites_urls: print site_url # Retrieve list of sitemaps submitted sitemaps = webmasters_service.sitemaps().list(siteUrl=site_url).execute() if 'sitemap' in sitemaps: sitemap_urls = [s['path'] for s in sitemaps['sitemap']] print " " + "\n ".join(sitemap_urls)
Step 4: Run the sample
After you have enabled the API in the Google Developers Console, installed the Google API client library, and set up the sample source code, the sample is ready to run. Running the sample from the command line returns a URL you need to visit in order to authorize the sample to access data on behalf of the current user.
Run the sample using
- Browse to the provided URL in your web browser.
- If you are not already logged into your Google account, you will be prompted to log in. If you are logged into multiple Google accounts, you will be asked to select one account to use for the authorization.
- Copy the code you're given after browsing to the link, and paste it into the prompt
Enter authorization code:. Click Enter.
When you finish these steps, the sample prints the primary email address for all the users in the domain.
If your goal is to expand the quickstart sample into something for your own installed application, consult the API Reference. The API Reference discusses all of the features of the Search Console API.