Java
Chilkat2 Java
Google Search Console API - Delete
See more Google Search Console Examples
Demonstrates how to remove a site from the set of the user's Search Console sites.Chilkat Java Downloads
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 sbUrlToDelete = new StringBuilder();
sbUrlToDelete.append("https://www.example.com/");
sbUrlToDelete.encode("url", "utf-8");
StringBuilder sbUrl = new StringBuilder();
sbUrl.append("https://www.googleapis.com/webmasters/v3/sites/");
sbUrl.appendSb(sbUrlToDelete);
// 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("DELETE", 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.");
}
}
}