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

How to add StructuredServiceItem in Google my business location using java?


Introduction: If you know what is Business Profile on Google so please skip introduction part.

As I have received notification for this question that this question needs more clarifications, I can explain a little more about what are the services, google categories and attributes of a location, here. However, the link I have provided is very obvious and who works on Google My business or the newer version which is called Google Business Profile has a very clear idea of what I am talking.

"With a Business Profile on Google, you can manage how your local business shows up across Google products, like Maps and Search. If you run a business that serves customers at a particular location, or you serve customers within a designated service area, your Business Profile can help people find you. Verified businesses on Google are twice as likely to be considered reputable.Business Profile on Google".

a location is described Location GBP Documentation, and contains main information about this location such as opening hours, phone number, email, categories etc., that some are obligatory and some of them are optional. My question is on serviceItems which is optional. the question is how to create it using JAVA. enter image description here

My Question:

Using The GMB documentation ,I am trying to define a service for my location in JAVA. I can add a FreeFormServiceItem service but not StructuredServiceItem. I have defined an enum for servicesTypes:

enum serviceTypes{
            BOOL,
            URL,
            REPEATED_ENUM,
            ENUM
        }
     JsonObject jsonUpdate = new JsonObject();
     JsonArray serviceItems = new JsonArray();
      String description = "something";
      serviceTypes serviceTypeId = serviceTypes.BOOL;  
     serviceItems.add(structuredServiceItem(serviceTypeId, description));
      jsonUpdate.add("serviceItems", serviceItems);

            OkHttpClient client = new OkHttpClient();
            String url = "https://mybusinessbusinessinformation.googleapis.com/v1/" + locationName + "?updateMask=" + updateMask + "&validateOnly=" + validateOnly;
            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, jsonUpdate.toString());

            Request request = new Request.Builder()
                    .url(url)
                    .patch(body)
                    .addHeader("Content-Type", "application/json")
                    .addHeader("Authorization", "Bearer " + access_token)
                    .build();

            try (Response response = client.newCall(request).execute()) {
                System.out.println("Response status code: " + response.code());
                System.out.println("Response body: " + response.body().string());
            } catch (Exception e) {
                e.printStackTrace();
            }

and then the structuredServiceItem :

 private static JsonObject structuredServiceItem(serviceTypes serviceTypeId, String description) {
            JsonObject structuredServiceItem = new JsonObject();
            structuredServiceItem.addProperty("serviceTypeId", serviceTypeId.toString()); 
            structuredServiceItem.addProperty("description", description);

            return structuredServiceItem;
        }

The error is

 Response body: {
  "error": {
    "code": 400,
    "message": "Invalid JSON payload received. Unknown name \"serviceTypeId\" at 'location.service_items[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"description\" at 'location.service_items[0]': Cannot find field.",
    "status": "INVALID_ARGUMENT",
    "details": [
      {
        "@type": "type.googleapis.com/google.rpc.BadRequest",
        "fieldViolations": [
          {
            "field": "location.service_items[0]",
            "description": "Invalid JSON payload received. Unknown name \"serviceTypeId\" at 'location.service_items[0]': Cannot find field."
          },
          {
            "field": "location.service_items[0]",
            "description": "Invalid JSON payload received. Unknown name \"description\" at 'location.service_items[0]': Cannot find field."
          }
        ]
      }
    ]
  }
}

Solution

  • Ok, I found the response. The problem was that I was defining serviceTypes incorrectly. By fetching Google Categories, it is possible to obtain the required information for defining StructuredServiceItem.

    Supposing that the variable mybusinessCategories takes care of credentials and also initials the API.

        MyBusinessBusinessInformation.Categories.List requestEn=mybusinessCategories.categories().list().setView("FULL").
                setLanguageCode("your languageCode").setRegionCode("your regionCode").setPageSize(100);
        ListCategoriesResponse ResponseEn=requestEn.execute();
        List<Category> categoriesEn=ResponseEn.getCategories();
        while (ResponseEn.getNextPageToken() != null) {
            requestEn.setPageToken(ResponseEn.getNextPageToken());
            ResponseEn=requestEn.execute();
            categoriesEn.addAll(ResponseEn.getCategories());    
        }
    
        for(Category cEn:categoriesEn) {
            if(cEn.containsKey("serviceTypes")) {  
                List<ServiceType> servicesEn =vcEn.getServiceTypes();
                for( ServiceType s: servicesEn) {
                    System.out.println( cEn.getName());
                    System.out.println(s.getServiceTypeId());
                    System.out.println( s.getDisplayName());
    
                }
            }