List, add and delete Sites with Google Search Java API
As you know, in Google Search Console you need to declare the Sites you own, they are called properties.
There are 2 types of properties:
Property Type | Description |
Domain | This type of asset is a domain name that you own. Once verified it will be tracked by Google Search Console. All domains including subdomains, with all protocols (HTTP or HTTPS) are tracked by it.
|
URL Prefix | This property is a part of a website designated by a prefix, for example:
Google Search Console only tracks URLs that match this property's prefix. |
In this article, I will guide you how to use the Google Search Console Java API to list, add and delete Sites on Google Search Console.
1. Requirements
The requirements for working with Google Search Java API are mentioned in the article below. This article also covers different ways to create an HttpRequestInitializer object (With API Key, OAuth Client Id or Service Account).
A utility class to create an HttpRequestInitializer object, which provides credentials for requests each time they are sent to Google:
MyUtils.java
package org.o7planning.googleapis.utils;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.io.FileUtils;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.json.JsonObjectParser;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.client.util.ObjectParser;
import com.google.auth.http.HttpCredentialsAdapter;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.auth.oauth2.ServiceAccountCredentials;
public class MyUtils {
public static final String SERVICE_ACCOUNT_FILE_PATH = "/Volumes/New/_gsc/test-google-search-console-key.json";
private static byte[] serviceAccountBytes;
public static HttpRequestInitializer createHttpRequestInitializer(String... scopes) throws IOException {
InputStream serviceAccountInputStream = getServiceAccountInputStream();
GoogleCredentials credentials = ServiceAccountCredentials //
.fromStream(serviceAccountInputStream) //
.createScoped(scopes);
HttpRequestInitializer requestInitializer = new HttpRequestInitializer() {
@Override
public void initialize(HttpRequest request) throws IOException {
HttpCredentialsAdapter adapter = new HttpCredentialsAdapter(credentials);
adapter.initialize(request);
//
if (request.getParser() == null) {
ObjectParser parser = new JsonObjectParser(GsonFactory.getDefaultInstance());
request.setParser(parser);
}
//
request.setConnectTimeout(60000); // 1 minute connect timeout
request.setReadTimeout(60000); // 1 minute read timeout
}
};
return requestInitializer;
}
public static synchronized InputStream getServiceAccountInputStream() throws IOException {
if (serviceAccountBytes == null) {
serviceAccountBytes = FileUtils.readFileToByteArray(new File(SERVICE_ACCOUNT_FILE_PATH));
}
return new ByteArrayInputStream(serviceAccountBytes);
}
}
A utility class for creating a SearchConsole.Sites object, an important object that is related to all the examples in this article.
MySiteUtils.java
package org.o7planning.googleapis.a14299.site;
import java.io.IOException;
import java.security.GeneralSecurityException;
import org.o7planning.googleapis.utils.MyUtils;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.services.searchconsole.v1.SearchConsole;
import com.google.api.services.searchconsole.v1.SearchConsoleScopes;
public class MySiteUtils {
private static final String APPLICATION_NAME = "My-Application";
public static SearchConsole.Sites getSitesClient(String applicationName) //
throws IOException, GeneralSecurityException {
//
// @scopes: WEBMASTERS or WEBMASTERS_READONLY
//
HttpRequestInitializer httpRequestInitializer = MyUtils
.createHttpRequestInitializer(SearchConsoleScopes.WEBMASTERS);
//
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
SearchConsole client = new SearchConsole.Builder( //
httpTransport, GsonFactory.getDefaultInstance(), httpRequestInitializer) //
.setApplicationName(applicationName) //
.build();
SearchConsole.Sites siteClient = client.sites();
return siteClient;
}
public static SearchConsole.Sites getSitesClient() //
throws IOException, GeneralSecurityException {
return getSitesClient(APPLICATION_NAME);
}
}
2. List Sites
Lists all Sites that have been added to your Google Search Console, including unverified Sites.
ListSiteExample.java
package org.o7planning.googleapis.a14299.site;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.List;
import com.google.api.services.searchconsole.v1.SearchConsole;
import com.google.api.services.searchconsole.v1.model.SitesListResponse;
import com.google.api.services.searchconsole.v1.model.WmxSite;
public class ListSiteExample {
public static void main(String[] args) throws IOException, GeneralSecurityException {
SearchConsole.Sites siteClient = MySiteUtils.getSitesClient();
SearchConsole.Sites.List listRequest = siteClient.list();
SitesListResponse listResponse = listRequest.execute();
System.out.println("Sites.List Response: " + listResponse.toPrettyString());
List<WmxSite> siteList = listResponse.getSiteEntry();
for (WmxSite site : siteList) {
System.out.println("\n- permissionLevel: " + site.getPermissionLevel());
System.out.println("- siteUrl: " + site.getSiteUrl());
}
}
}
Output:
Sites.List Response: {
"siteEntry": [
{
"permissionLevel": "siteOwner",
"siteUrl": "https://www.langlearning.net/"
},
{
"permissionLevel": "siteOwner",
"siteUrl": "sc-domain:langlearning.net"
},
{
"permissionLevel": "siteOwner",
"siteUrl": "https://langlearning.net/"
}
]
}
- permissionLevel: siteOwner
- siteUrl: https://www.langlearning.net/
- permissionLevel: siteOwner
- siteUrl: sc-domain:langlearning.net
- permissionLevel: siteOwner
- siteUrl: https://langlearning.net/
3. View information for a specific Site
For example, get information about a specific Site, including your permissions for this Site.
Possible values of permissionLevel:
- "siteFullUser"
- "siteOwner"
- "siteRestrictedUser"
- "siteUnverifiedUser"
GetSiteExample.java
package org.o7planning.googleapis.a14299.site;
import java.io.IOException;
import java.security.GeneralSecurityException;
import org.o7planning.googleapis.MyTestConstants;
import com.google.api.services.searchconsole.v1.SearchConsole;
import com.google.api.services.searchconsole.v1.model.WmxSite;
public class GetSiteExample {
private static void getOneSiteInfo(String siteId) throws IOException, GeneralSecurityException {
SearchConsole.Sites siteClient = MySiteUtils.getSitesClient();
SearchConsole.Sites.Get getRequest = siteClient.get(siteId);
WmxSite site = getRequest.execute();
System.out.println("\nSite: " + site.toPrettyString());
System.out.println("- permissionLevel: " + site.getPermissionLevel());
System.out.println("- siteUrl: " + site.getSiteUrl());
}
public static void main(String[] args) throws IOException, GeneralSecurityException {
//
// @siteUrl:
// https://langlearning.net/ (Needs suffix "/") (for a URL-prefix property)
// sc-domain:langlearning.net (for a Domain property)
//
getOneSiteInfo(MyTestConstants.siteUrl);
}
}
Output:
Site: {
"permissionLevel": "siteOwner",
"siteUrl": "https://langlearning.net/"
}
- permissionLevel: siteOwner
- siteUrl: https://langlearning.net/
4. Add a Site
Next, in this section we will use the Google Search API to add a Site to Google Search Console. In some cases they will be automatically verified, for example:
- langlearning.net (Sc Domain property) Verified!
- ---> https://langlearning.net/app/en/ (URL Prefix property) verified.
- ---> http://www.langlearning.net/ (URL Prefix property) verified.
Hoặc:
- http://www.langlearning.net (URL Prefix property) Verified!
- ---> http://www.langlearning.net/app/en/ (URL Prefix property) Verified!
Note: Google Search API does not support verifying a Site. If you are interested in this, use the Google Site Verification API.
- Java Google Site Verification API
AddSiteExample.java
package org.o7planning.googleapis.a14299.site;
import java.io.IOException;
import java.security.GeneralSecurityException;
import org.o7planning.googleapis.MyTestConstants;
import com.google.api.services.searchconsole.v1.SearchConsole;
public class AddSiteExample {
//
// @siteUrl:
// https://langlearning.net/ (Needs suffix "/") (for a URL-prefix property)
// https://sub1.bar.com/ (Needs suffix "/") (for a URL-prefix property)
// sc-domain:foo.com (for a Domain property)
//
private static void addSite(String siteUrl) throws IOException, GeneralSecurityException {
SearchConsole.Sites siteClient = MySiteUtils.getSitesClient();
SearchConsole.Sites.Add addRequest = siteClient.add(siteUrl);
addRequest.execute();
System.out.println("Done!");
}
public static void main(String[] args) throws IOException, GeneralSecurityException {
//
// @siteUrl: sc-domain:bar.com
// @siteUrl: http://www.bar.com/
//
addSite(MyTestConstants.siteUrlToAdd1);
}
}
5. Delete a Site
For example, deleting a Site:
DeleteSiteExample.java
package org.o7planning.googleapis.a14299.site;
import java.io.IOException;
import java.security.GeneralSecurityException;
import com.google.api.services.searchconsole.v1.SearchConsole;
public class DeleteSiteExample {
private static void deleteSite(String siteId) throws IOException, GeneralSecurityException {
SearchConsole.Sites siteClient = MySiteUtils.getSitesClient();
SearchConsole.Sites.Delete deleteRequest = siteClient.delete(siteId);
deleteRequest.execute();
System.out.println("Done!");
}
public static void main(String[] args) throws IOException, GeneralSecurityException {
// @siteUrl:
// https://langlearning.net/ (Needs suffix "/") (for a URL-prefix property)
// https://sub1.bar.com/ (Needs suffix "/") (for a URL-prefix property)
// sc-domain:foo.com (for a Domain property)
//
String siteId = "https://www.langlearning.net/";
deleteSite(siteId);
}
}
No ADS
- Creating a Google API Console project and OAuth2 Client ID
- Register Google Map API Key
- Get Started with the Google Search Console API
- Index pages with the Java Google Indexing API
- Use Google reCAPTCHA in Java Web Application
- Manipulating files and folders on Google Drive using Java
- Create a Google Service Account
- List, add and delete Sites with Google Search Java API
- List, submit and delete Sitemaps with Google Search Java API
Show More