{"id":659,"date":"2020-05-21T13:47:54","date_gmt":"2020-05-21T17:47:54","guid":{"rendered":"http:\/\/bhoey.com\/blog\/?p=659"},"modified":"2020-06-25T14:09:15","modified_gmt":"2020-06-25T18:09:15","slug":"google-search-console-api-java-example","status":"publish","type":"post","link":"https:\/\/bhoey.com\/blog\/google-search-console-api-java-example\/","title":{"rendered":"Google Search Console API - Java Example"},"content":{"rendered":"<h3>Background<\/h3>\n<p><a href=\"https:\/\/search.google.com\/search-console\">Google Search Console<\/a> is an online tool provided by Google to manage how your site relates specifically to people searching with the Google Search Engine. The information it offers is somewhat similar to a subset of what Google Analytics provides but allows you to submit sitemaps and perform other indexing-specific operations not available in Analytics.<\/p>\n<p>In my workflow I mostly use the Search Console as a quick overall snapshot to see how the site as a whole is performing on the search rankings and what search terms people are using to get there. As I like to automate operations I find myself doing manually on a regular basis, I thought it would be useful to put together a small checking program that would notify me when the new stats had been made available instead of checking the page periodically.<\/p>\n<p>Google provides a <a href=\"https:\/\/developers.google.com\/webmaster-tools\/search-console-api-original\">REST API for Search Console<\/a> along with several language libraries that simplify interacting with it. There are quickstart guides for python and Java, however I found the Java examples somewhat lacking and as a result decided to put together this post to help augment the documentation Google provides.<\/p>\n<p>Below are instructions on how to set up and access the Search Console API using a service account authenticated Java program.<\/p>\n<p>Note: This example depends on your site already added in the Search Console, official instructions on how to do that are available <a href=\"https:\/\/support.google.com\/webmasters\/answer\/34592?hl=en\">here<\/a><\/p>\n<h3>Setting Up The Service Account<\/h3>\n<p>Your application will need to authenticate its requests in order to perform operations against sites you have set up in Google Search Console. A comparison of the different Google authentication schemes is beyond the scope of this post, but suffice it to say a service account is a pseudo-user account meant for applications that can be given access to services similar to the process for a real user account.<\/p>\n<p>You can create a service account for your application by going to the <a href=\"https:\/\/console.developers.google.com\/apis\/credentials\">API Credentials<\/a> page and clicking on Create Credentials then selecting service account.<\/p>\n<p><img loading=\"lazy\" class=\"aligncenter size-full wp-image-674\" src=\"http:\/\/bhoey.com\/blog\/wp-content\/uploads\/2020\/05\/Google-API-Create-Service-Account-Credentials.png\" alt=\"Create Service Account Screenshot\" width=\"900\" height=\"223\" srcset=\"https:\/\/bhoey.com\/blog\/wp-content\/uploads\/2020\/05\/Google-API-Create-Service-Account-Credentials.png 900w, https:\/\/bhoey.com\/blog\/wp-content\/uploads\/2020\/05\/Google-API-Create-Service-Account-Credentials-300x74.png 300w, https:\/\/bhoey.com\/blog\/wp-content\/uploads\/2020\/05\/Google-API-Create-Service-Account-Credentials-768x190.png 768w\" sizes=\"(max-width: 900px) 100vw, 900px\" \/><\/p>\n<p>You'll then be able to set the name of your service account along with some other optional settings.<\/p>\n<p>At the end of the process you will be able to download the service account JSON credential file that will be needed when running the example below. Note: This file will allow access to any services that have been granted to the service account and should be secured and protected appropriately.<\/p>\n<h3>Granting The Service Account Access to Search Console<\/h3>\n<p>Log into Search Console with an account that has owner level access. Click the Settings menu item at the bottom left of the page, and then click on Users and Permissions row in the loaded page. At the top right you should now see an Add User button, click that and paste the generated email address for the service account you created earlier. The Permission field allows you to select Full or Restricted, the difference between them is documented <a href=\"https:\/\/support.google.com\/webmasters\/answer\/7687615?hl=en\">here<\/a>, but if you are only querying data you'll likely want to choose Restricted.<\/p>\n<p><img loading=\"lazy\" class=\"aligncenter size-full wp-image-685\" src=\"http:\/\/bhoey.com\/blog\/wp-content\/uploads\/2020\/05\/Google-Search-Console-Add-User.png\" alt=\"Google Search Console - Add User Screenshot\" width=\"598\" height=\"304\" srcset=\"https:\/\/bhoey.com\/blog\/wp-content\/uploads\/2020\/05\/Google-Search-Console-Add-User.png 598w, https:\/\/bhoey.com\/blog\/wp-content\/uploads\/2020\/05\/Google-Search-Console-Add-User-300x153.png 300w\" sizes=\"(max-width: 598px) 100vw, 598px\" \/><\/p>\n<h3>Adding Search Console API Maven Dependency<\/h3>\n<p>With the service account configuration out of the way we can now turn to the coding side. To use the Search Console Java API you'll need to add the following dependency to your Maven pom.xml:<\/p>\n<pre class=\"brush: xml; notranslate\">        \n\n    &lt;dependency&gt;\n        &lt;groupId&gt;com.google.apis&lt;\/groupId&gt;\n        &lt;artifactId&gt;google-api-services-webmasters&lt;\/artifactId&gt;\n        &lt;version&gt;v3-rev35-1.25.0&lt;\/version&gt;\n    &lt;\/dependency&gt;\n\n<\/pre>\n<h3>Java Example: Retrieving Daily Statistics<\/h3>\n<p>The example below will print site-level daily statistics such as impressions, clicks, and CTR for any site the service account has been granted access to.<\/p>\n<pre class=\"brush: java; notranslate\">import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;\nimport com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;\nimport com.google.api.client.http.HttpTransport;\nimport com.google.api.client.json.JsonFactory;\nimport com.google.api.client.json.jackson2.JacksonFactory;\nimport com.google.api.services.webmasters.Webmasters;\nimport com.google.api.services.webmasters.WebmastersScopes;\nimport com.google.api.services.webmasters.model.ApiDataRow;\nimport com.google.api.services.webmasters.model.SearchAnalyticsQueryRequest;\nimport com.google.api.services.webmasters.model.SearchAnalyticsQueryResponse;\nimport com.google.api.services.webmasters.model.SitesListResponse;\nimport com.google.api.services.webmasters.model.WmxSite;\nimport java.io.FileInputStream;\nimport java.util.Arrays;\nimport java.util.Collections;\nimport java.util.List;\n\n\/\/ Source https:\/\/bhoey.com\/blog\/google-search-console-api-java-example\/\n\npublic class SearchConsoleAPIExample {\n\n    public static void main(String[] args) throws Exception {\n\n        String keyFilePath = \"\/path\/to\/your\/service_account.json\";\n\n        List&lt;String&gt; scopes = Collections.singletonList(WebmastersScopes.WEBMASTERS_READONLY);\n\n        GoogleCredential credentials = GoogleCredential\n                .fromStream(new FileInputStream(keyFilePath))\n                .createScoped(scopes);\n\n        SearchAnalyticsQueryRequest queryRequest = new SearchAnalyticsQueryRequest();\n        queryRequest.setSearchType(null); \/\/ override 'web' filter default\n        queryRequest.setStartDate(\"2020-05-01\");\n        queryRequest.setEndDate(\"2020-05-20\");\n\n        List&lt;String&gt; dimensions = Arrays.asList(\n                \/\/\"page\",             \/\/ An explanation of dimensions is available at:\n                \/\/\"query\",            \/\/\n                \/\/\"country\",          \/\/ https:\/\/developers.google.com\/webmaster-tools\/search-console-api-original\/v3\/\n                \/\/\"devide\",           \/\/    searchanalytics\/query#dimensionFilterGroups.filters.dimension\n                \/\/\"searchAppearance\", \/\/\n                \"date\"                \/\/ Note 'date' isn't included in the list above, but is available nonetheless\n        );\n\n        queryRequest.setDimensions(dimensions);\n\n        HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();\n        JsonFactory jsonFactory = new JacksonFactory();\n        Webmasters service = new Webmasters.Builder(httpTransport, jsonFactory, credentials)\n                .setApplicationName(\"MyAppName\")\n                .build();\n\n        Webmasters.Sites.List siteListRequest = service.sites().list();\n\n        SitesListResponse siteListResponse = siteListRequest.execute();\n        for (WmxSite site : siteListResponse.getSiteEntry()) {\n\n            String url = site.getSiteUrl();\n\n            Webmasters.Searchanalytics.Query query = service.searchanalytics().query(url, queryRequest);\n\n            SearchAnalyticsQueryResponse queryResponse = query.execute();\n\n            \/\/ Print site header\n            System.out.println(\"----[ \" + url + \" ]----\");\n\n            \/\/ Print full JSON response\n            System.out.println(queryResponse.toPrettyString());\n\n            if (queryResponse.getRows() == null){\n                System.out.println(\"No rows returned in response\");\n            }\n            else {\n                for (ApiDataRow row : queryResponse.getRows()) {\n                    \/\/ Print a single row\n                    System.out.println(row);\n\n                    \/\/ Print an individual data point\n                    System.out.println(row.getClicks().intValue());\n                }\n            }\n        }\n    }\n}\n<\/pre>\n<p>There's a lot going on in this short program, for more information on the individual classes and function calls you can look through <a href=\"https:\/\/developers.google.com\/resources\/api-libraries\/documentation\/webmasters\/v3\/java\/latest\/\">the API's official Javadocs<\/a> and\/or <a href=\"https:\/\/github.com\/googleapis\/google-api-java-client-services\/tree\/master\/clients\/google-api-services-webmasters\/v3\">source code<\/a>.<\/p>\n<h3>Conclusion<\/h3>\n<p>Hopefully this example fills in some of the blanks of how to use the Search Console Java API with a service account. Good luck and happy coding!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Google provides a REST API for Search Console along with several language libraries that simplify interacting with it. There are quickstart guides for python and Java, however I found the Java examples somewhat lacking and as a result decided to put together this post to help augment the documentation Google provides.&nbsp;<a href=\"https:\/\/bhoey.com\/blog\/google-search-console-api-java-example\/\">[Continue&nbsp;reading...] <span class=\"screen-reader-text\">Google Search Console API - Java Example<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":830,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[75,71],"tags":[57,72,73,74],"_links":{"self":[{"href":"https:\/\/bhoey.com\/blog\/wp-json\/wp\/v2\/posts\/659"}],"collection":[{"href":"https:\/\/bhoey.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bhoey.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bhoey.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/bhoey.com\/blog\/wp-json\/wp\/v2\/comments?post=659"}],"version-history":[{"count":41,"href":"https:\/\/bhoey.com\/blog\/wp-json\/wp\/v2\/posts\/659\/revisions"}],"predecessor-version":[{"id":706,"href":"https:\/\/bhoey.com\/blog\/wp-json\/wp\/v2\/posts\/659\/revisions\/706"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/bhoey.com\/blog\/wp-json\/wp\/v2\/media\/830"}],"wp:attachment":[{"href":"https:\/\/bhoey.com\/blog\/wp-json\/wp\/v2\/media?parent=659"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bhoey.com\/blog\/wp-json\/wp\/v2\/categories?post=659"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bhoey.com\/blog\/wp-json\/wp\/v2\/tags?post=659"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}