Sample code for 30+ languages & platforms
Java

Google Search Console API - List

See more Google Search Console Examples

Lists the user's Search Console sites.

Chilkat Java Downloads

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

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"));

        String responseStr;
        try {
            responseStr = http.quickGetStr("https://www.googleapis.com/webmasters/v3/sites");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

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

        // Sample response:

        // {
        //  "siteEntry": [
        //   {
        //    "siteUrl": "https://www.example.com/",
        //    "permissionLevel": "siteUnverifiedUser"
        //   },
        //   {
        //    "siteUrl": "http://www.chilkatsoft.com/",
        //    "permissionLevel": "siteOwner"
        //   }
        //  ]
        // }

        System.out.println(responseStr);

        // Use this online tool to generate parsing code from sample JSON:
        // Generate Parsing Code from JSON

        JsonObject json = new JsonObject();

        json.load(responseStr);

        String siteUrl = "";
        String permissionLevel = "";

        int i = 0;
        int count_i = json.sizeOfArray("siteEntry");
        while (i < count_i) {
            json.setI(i);
            siteUrl = json.stringOf("siteEntry[i].siteUrl");
            System.out.println("siteUrl: " + siteUrl);
            permissionLevel = json.stringOf("siteEntry[i].permissionLevel");
            System.out.println("permissionLevel: " + permissionLevel);
            i = i + 1;
        }
    }
}