To use the command line to access the Google Search Console API, follow the steps in this tutorial.
Using the command line allows you to try aspects of the API without having to create a full application.
OACurl is a Java-based command-line wrapper for curl
that authenticates using various versions of OAuth.
If you like command lines, it's a simple way to get started.
Because it's based on command lines, this example is useful for personal scripts, but not
ideal for web-applications.
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.
Your application's client IDs and service account keys are now listed on the Credentials page. For details, click a client ID; parameters vary depending on the ID type, but might include email address, client secret, JavaScript origins, or redirect URIs.
Take note of the Client ID as you'll need to add it to your code later.
Step 2: Install OACurl
Learn about and download OACurl on Google Project Hosting.
Alternately, you can also build OACurl from source.
Step 3: Authenticate with OACurl
Authenticate with OACurl using the unique Client ID and Client Secret
you created in the "Enable the Search Console API" step. Replace YOUR_CLIENT_ID with
your Client ID and YOUR_CLIENT_SECRET with your Client Secret then run the
following command line to authenticate.
$ java -cp oacurl-1.3.0.jar com.google.oacurl.Login \
--scope https://www.googleapis.com/auth/webmasters --oauth2 \
--consumer-key=YOUR_CLIENT_ID --consumer-secret=YOUR_CLIENT_SECRET
This opens a browser window with the authentication request.
After granting access, the OAuth 2.0 token is stored locally in ~/.oacurl.properties.
Now you can explore Google Search Console API using oacurl.
Step 4: Explore the API
After authenticating and storing the access token, you can use OACurl to access all parts of the API. For example, to list all sites in your Google Search Console account, try the following:
$ java -cp oacurl-1.3.0.jar com.google.oacurl.Fetch "https://www.googleapis.com/webmasters/v3/sites"
Or, to list the sitemap files submitted for a website verified in your account,
try the following.
Replace http%3A%2F%2Fexample.com%2F with the
percent-encoded URL of your website.
$ java -cp oacurl-1.3.0.jar com.google.oacurl.Fetch \
"https://www.googleapis.com/webmasters/v3/sites/http%3A%2F%2Fexample.com%2F/sitemaps"
Next Steps
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 Google Search Console API.