Search code examples
javagoogle-my-business-apigoogle-business-profile-api

How to send request to Business Profile Performance API in java for getting search keywords?


I am using Business Profile Performance API to get the search keywords used to find a business in search or maps. Supposing business_profile_performance is the initializer variable of type BusinessProfilePerformance API which also takes care of credentials:

                try{ 
                   BusinessProfilePerformance.Locations.Searchkeywords.Impressions.Monthly.List impressionsList= business_profile_performance.
locations().searchkeywords().impressions().monthly().list("locations/"+locationName);               
                ListSearchKeywordImpressionsMonthlyResponse response = impressionsList.execute();
                                        List<SearchKeywordCount>  impressions = response.getSearchKeywordsCounts();
                                         System.out.println("Size is "+ impressions.size());
                } catch (IOException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    }

It gives me error :

{
  "code" : 400,
  "details" : [ {
    "@type" : "type.googleapis.com/google.rpc.BadRequest"
  } ],
  "errors" : [ {
    "domain" : "global",
    "message" : "Request contains an invalid argument.",
    "reason" : "badRequest"
  } ],
  "message" : "Request contains an invalid argument.",
  "status" : "INVALID_ARGUMENT"
}

Any help would be appreciated. Thank you.


Solution

  • Ok I found the answer, I was not sending months range:

    LocalDate localDate = LocalDate.now();
            
            localDate= localDate.minusMonths(1);
            int endYear= localDate.getYear() ;
            int endMonth= localDate.getMonthValue() ;
            
            localDate= localDate.minusMonths(1);
            int startYear= localDate.getYear() ;
            int startMonth= localDate.getMonthValue() ;
         
            try {
    
                BusinessProfilePerformance.Locations.Searchkeywords.Impressions.Monthly.List impressions
                = business_profile_performance.locations().searchkeywords().impressions().monthly().list("locations/"+locationName)
                
                .setMonthlyRangeEndMonthYear(endYear)
                .setMonthlyRangeEndMonthMonth(endMonth)
                 
                .setMonthlyRangeStartMonthYear(startYear)
                .setMonthlyRangeStartMonthMonth(startMonth);
    
                ListSearchKeywordImpressionsMonthlyResponse response = impressions.execute();    
                System.out.println(response.toString());
    
    
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }