You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Java在Google Business Profile中添加StructuredServiceItem

Google Business Profile API:Java创建StructuredServiceItem失败问题解决

引言

若您熟悉Google Business Profile(原Google My Business)可直接查看问题部分。

Google Business Profile用于管理本地商家在谷歌地图、搜索等产品中的展示,有实体地址或服务区域的商家可通过它触达客户,已验证的商家可信度是未验证的两倍。商家位置信息包含营业时间、电话、邮箱、分类等必填/可选字段,本次聚焦可选字段serviceItems的StructuredServiceItem创建问题。

问题详情

尝试用Java调用Google Business Profile API为商家位置添加服务时,FreeFormServiceItem可成功创建,但StructuredServiceItem始终报错。

当前代码实现:
定义了serviceTypes枚举:

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();
}

对应的structuredServiceItem方法:

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

    return structuredServiceItem;
}

调用后返回错误:

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."
          }
        ]
      }
    ]
  }
}

问题原因及解决方案

错误提示明确指出serviceTypeId和description字段不存在,根源在于StructuredServiceItem的JSON结构不符合API要求:需嵌套在指定字段下,且要使用官方定义的服务类型ID,同时匹配对应的值类型结构。

修正后的代码

  1. 重构structuredServiceItem方法,按照API规范构建JSON结构:
private static JsonObject structuredServiceItem(String officialServiceTypeId, boolean boolValue) {
    JsonItem = new JsonObject();
    // 按API要求,将StructuredServiceItem嵌套在指定字段中
    JsonObject structuredServiceItem = new JsonObject();
    structuredServiceItem.addProperty("serviceTypeId", officialServiceTypeId);
    // 根据服务类型添加对应值(此处以BOOL类型为例)
    JsonObject value = new JsonObject();
    value.addProperty("boolValue", boolValue);
    structuredServiceItem.add("value", value);

    item.add("structuredServiceItem", structuredServiceItem);
    return item;
}
  1. 调用时使用官方服务类型ID(需替换为实际业务对应的官方ID):
JsonObject jsonUpdate = new JsonObject();
JsonArray serviceItems = new JsonArray();
// 替换为Google官方定义的布尔类型服务ID
String officialServiceId = "your.official.bool.service.id";
serviceItems.add(structuredServiceItem(officialServiceId, true));
jsonUpdate.add("serviceItems", serviceItems);

// 后续请求代码保持不变...

关键注意点

  • 字段嵌套规则:StructuredServiceItem必须作为serviceItems数组元素的structuredServiceItem字段值传递,不能直接平铺字段。
  • 官方服务ID:必须使用Google提供的官方服务类型ID,自定义枚举的字符串无法被API识别。
  • 值类型匹配:不同服务类型对应不同的值字段(如boolValue、urlValue、enumValue等),需根据服务类型正确设置。

内容的提问来源于stack exchange,提问作者Shila Mosammami

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 12:35:04