Sample code for 30+ languages & platforms
Java

Google Search Console API - Add

See more Google Search Console Examples

Demonstrates how to add a site to the set of the user's sites in Search Console.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.StringBuilder;

public class ChilkatExample {

    public static void main(String[] args) {
        // This example requires the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        // This example uses a previously obtained access token having permission for the
        // Google Search Console scope.

        // In this example, Get a Google Search Console OAuth2 Access Token, the access
        // token was saved to a JSON file.  This example fetches the access token from the file..
        JsonObject jsonToken = new JsonObject();
        jsonToken.loadFile("qa_data/tokens/googleSearchConsole.json");
        if (!jsonToken.hasMember("access_token")) {
            System.out.println("No access token found.");
            return;
        }

        Http http = new Http();
        http.setAuthToken(jsonToken.stringOf("access_token"));

        StringBuilder sbUrlToAdd = new StringBuilder();
        sbUrlToAdd.append("https://www.example.com/");
        sbUrlToAdd.encode("url", "utf-8");

        StringBuilder sbUrl = new StringBuilder();
        sbUrl.append("https://www.googleapis.com/webmasters/v3/sites/");
        sbUrl.appendSb(sbUrlToAdd);

        // Our URL looks like this:  https://www.googleapis.com/webmasters/v3/sites/https%3A%2F%2Fwww.example.com%2F
        System.out.println(sbUrl.getAsString());

        String url = sbUrl.getAsString();
        HttpResponse resp = new HttpResponse();
        try {
            http.httpNoBody("PUT", url, resp);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        int statusCode = resp.getStatusCode();
        System.out.println("Response Status Code: " + statusCode);

        if (statusCode >= 400) {
            System.out.println(resp.getBodyStr());
            System.out.println("Failed.");
        }

        System.out.println("Success");
    }
}