javagoogle-my-business-apigoogle-my-businessgoogle-business-profile-apigoogle-business-profile

How to send request to Business Profile Performance API in Java?


Does anyone know how I can send request to this API using Java? The link is Business Profile Performance API but there is no code example. The code I tried is not correct: supposing businessProfilePerformance is a variable of type BusinessProfilePerformance to initialise global parameters via its Builder and also takes care of authorisation.

try {

BusinessProfilePerformance.Locations.GetDailyMetricsTimeSeries metricReport = businessProfilePerformance.locations()
                                .getDailyMetricsTimeSeries(locationName);
                        GetDailyMetricsTimeSeriesResponse response = metricReport.execute();
                        if (!response.isEmpty()) {
                            System.out.println("Response== " + response);
                        }

                    } catch (Exception e) {
                        System.out.println(e);
                    }

The error is

com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request

{
  "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"
}

Thanks


Solution

  • I found the answer. I put it here in case might serve at anyone: supposing that the variable business_profile_performance is the initialiser global parameters via its Builder and also takes care of authentication.

    try {
    
                BusinessProfilePerformance.Locations.GetDailyMetricsTimeSeries metricReport =
                        business_profile_perfomance.locations().
                        getDailyMetricsTimeSeries("My locationID" ).
                        setDailyMetric(dailyMetric).
                        setDailyRangeStartDateYear(Integer.valueOf("the desired start Year")).
                        setDailyRangeStartDateMonth(Integer.valueOf(" the desired start Month" )).
                        setDailyRangeStartDateDay(Integer.valueOf(" the desired start Day ")).
    
                        setDailyRangeEndDateYear(Integer.valueOf("the desired End Year" )).
                        setDailyRangeEndDateMonth(Integer.valueOf(" the desired End Month")).
                        setDailyRangeEndDateDay(Integer.valueOf("the desired End Day"));
    
                GetDailyMetricsTimeSeriesResponse response = metricReport.execute();
                if (!response.isEmpty()) {
                    System.out.println(dailyMetric + " Response == " + response);
                }
    
            } catch (Exception e) {
                System.out.println(e);
            }
    

    the variable dailyMetric can be one of the metrics written in the photo below:

    enter image description here