Categories
GCP Java

Google search console Java API, with OAuth code flow or Service Account

In this tutorial, we explain how to connect and use the Google search console Java API.

We first connect to the API using either the OAuth authorization code flow, or a GCP Service Account. In particular, we describe how to save a StoredCredential on disk, and how to get a refresh token by setting accessType = “offline” and approvalPrompt = “force”.

Then we retrieve data from the API. Finally, we list the sitemaps URLs. But we could also query analytics data the same way.

1) Dependencies in pom.xml

In your pom.xml, add the following dependencies:

<dependency>
    <groupId>com.google.auth</groupId>
    <artifactId>google-auth-library-oauth2-http</artifactId>
    <version>1.2.2</version>
</dependency>
<dependency>
    <groupId>com.google.apis</groupId>
    <artifactId>google-api-services-searchconsole</artifactId>
    <version>v1-rev20211026-1.32.1</version>
</dependency>

2) Connect to the API using the Oauth authorization code flow

First you need to gather the following connection constants:

String ID = "your-oauth-client-id";
String SECRET = "your-oauth-client-secret";

String OAUTH_SCOPE = "https://www.googleapis.com/auth/webmasters.readonly";
String USER_ID = "foo";

Replace ID and SECRET with your own OAuth client id and secret. You can get them by creating new credentials on the Google search console API page. Choose OAuth Client ID and Application type = “Desktop app”.

Then the code to get a credential for the API is (see this page for more information):

static HttpRequestInitializer credFromOAuth() throws IOException {
    String REDIRECT_URI = "urn:ietf:wg:oauth:2.0:oob";
    HttpTransport httpTransport = new NetHttpTransport();
    JsonFactory jsonFactory = GsonFactory.getDefaultInstance();

    DataStoreFactory dsf = new FileDataStoreFactory(new File(System.getProperty("user.home") + "/Desktop/googleCreds"));
    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(httpTransport, jsonFactory, ID, SECRET, List.of(OAUTH_SCOPE))
                .setDataStoreFactory(dsf)
                .setAccessType("offline")
                .setApprovalPrompt("force")
                .build();

    Credential cred = flow.loadCredential(USER_ID);
    if (cred != null)
        return cred;

    String url = flow.newAuthorizationUrl().setRedirectUri(REDIRECT_URI).build();
    System.out.println("Please open the following URL in your browser then type the authorization code:");
    System.out.println("  " + url);
    System.out.println("Enter authorization code:");
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String code = br.readLine();

    GoogleTokenResponse response = flow.newTokenRequest(code).setRedirectUri(REDIRECT_URI).execute();
    return flow.createAndStoreCredential(response, USER_ID);
}

The first time you run this code, follow the login link that is printed in the console, login using your Google account, copy the generated authorization code, and paste it into the console. Finally, press enter.

The next times you run this code, the Google Java SDK will use the datastore on your local computer, in this folder:

System.getProperty("user.home") + "/Desktop/googleCreds"

Then, the previous credential will be re used. If it expired, the Google Java SDK will automatically fetch a new access token by using the refresh token from the store.

A refresh token is always present because we set the following parameters in the GoogleAuthorizationCodeFlow:

.setAccessType("offline")
.setApprovalPrompt("force")

3) Connect to the API using a GCP Service Account (alternative to 2))

Start with a GCP Service Account that you created in the Google search console API page and to which you gave access permission in the Google search console (for the website you own).

Then gather the following connection constants:

String SERV_ACCOUNT_JSON = "path/to/your/service/account/json";

String OAUTH_SCOPE = "https://www.googleapis.com/auth/webmasters.readonly";

Finally, to get a credential for the Google search console API, simply run:

static HttpRequestInitializer credFromServiceAccount() throws IOException {
    GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream(SERV_ACCOUNT_JSON)
                .createScoped(List.of(OAUTH_SCOPE));
    return new HttpCredentialsAdapter(credentials);
}

4) List sitemaps paths using the Google search console API

To list all the sitemaps paths for the website eazytutorial.com, run:

// choose the credential method you want from part 2) or 3)
// HttpRequestInitializer credential = credFromOAuth();
HttpRequestInitializer credential = credFromServiceAccount();

HttpTransport httpTransport = new NetHttpTransport();
JsonFactory jsonFactory = GsonFactory.getDefaultInstance();
SearchConsole service = new SearchConsole.Builder(httpTransport, jsonFactory, credential)
                .build();

SitesListResponse siteList = service.sites().list().execute();
String siteUrl = null;
for (WmxSite currentSite : siteList.getSiteEntry()) {
    String siteUrlCand = currentSite.getSiteUrl();
    if (siteUrlCand.contains("eazytutorial.com"))
        siteUrl = siteUrlCand;
}

SitemapsListResponse siteMapList = service.sitemaps().list(siteUrl).execute();

for (WmxSitemap w : siteMapList.getSitemap())
    System.out.println(w.getPath());

In the console, we get the following result:

https://eazytutorial.com/index.php/post-sitemap.xml?a=a
https://eazytutorial.com/index.php/post-sitemap.xml
https://eazytutorial.com/index.php/sitemap_index.xml
http://www.eazytutorial.com/atom.xml?redirect=false&start-index=1&max-results=500

That’s it for this article ! If you have any question, please leave a reply below, we answer within 24h.

Leave a Reply

Your email address will not be published. Required fields are marked *