Using Java to Bulk Submit URLs to Google Search Console

Java Bulk Submit URLs to Google Search Console

Today in this article, we will learn how to perform Java-Bulk Submit URLs to Google Search Console. We will Java example to Bulk Submit URLs to Google Search Engine/Console for Indexing or Crawling purpose.

We already learned how to set up the service account, project, and other configurations so that we can leverage Google’s Indexing API before.

Today in this article, we will cover below aspects,

If you have not performed mandatory prerequisites, please follow the below article for more details,

You may find the need to submit the URLs due to many reasons like article update due to new data or upgrade or article enhancement, etc. In such scenarios, it is always difficult to perform indexing manually.

We shall be using below endpoints required for publishing the URLs,

Step1- Define the scope and endpoint

SCOPES = [ “https://www.googleapis.com/auth/indexing ]

ENDPOINT = “https://indexing.googleapis.com/v3/urlNotifications:publish

In your Java code please define the scopes and endpoint as below,

String scopes = "https://www.googleapis.com/auth/indexing";
String endPoint = "https://indexing.googleapis.com/v3/urlNotifications:publish";

Step2 – Include the service account access token

Please include the service account access token in your code. In the last article, we generated a service account token thecodebuzz-da659b2b8b0d.json which is the private key that you use to authenticate the service account using OAuth2.

For more details :

  • Create Service account JSON for Google Indexing API
  • Step3 – Define the body of the request and invoke API

    Below is a sample Java code that can be used to send Bulk URLs to Indexing API

    JsonFactory jsonFactory = new JacksonFactory();
    
    // thecodebuzz-da659b2b8b0d.json is the private key that you created for your service account.
    InputStream in = IOUtils.toInputStream("thecodebuzz-da659b2b8b0d.json");
    
    GoogleCredential credentials =
      GoogleCredential.fromStream(in, this.httpTransport, jsonFactory).createScoped(Collections.singleton(scopes));
    
    GenericUrl genericUrl = new GenericUrl(endPoint);
    HttpRequestFactory requestFactory = this.httpTransport.createRequestFactory();
    
    // Define all URLs to be submitted to API
    
    String content = "{"
      + "\"url\": \"http://your url .com\","
      + "\"type\": \"URL_UPDATED\","
      + "}";
    
    
    HttpRequest request =
      requestFactory.buildPostRequest(genericUrl, ByteArrayContent.fromString("application/json", content));
    
    credentials.initialize(request);
    HttpResponse response = request.execute();
    int statusCode = response.getStatusCode();
    
    
    
    

    • In the above code content is defined to contain the URL to be submitted.
    • We have to define request format as “application/json”
    • getStatusCode lets you verify the status code after executing the API

    Once successfully submitted the URLs for indexing, you can see the status code as 200 for the request.

    JAVA for Bulk Submit URLs to Google indexingindex url google using script index url google search engine seosubmit to google for indexingadd to google index google add url to index google to crawl and index your url submit url for indexing bulk url indexer submit url for indexing google add website to index google indexing api python bulk request indexing google google submit url for indexing google submit page for indexing google console index

    References :

    Please follow the below article for more details on prerequisites,

    Do you have any comments or ideas or any better suggestions to share?

    Please sound off your comments below.

    Happy Coding !!



    Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



    2 thoughts on “Java-Bulk Submit URLs to Google Search Console

    Leave a Reply

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