返回

如何在 Google 我的商家中使用 Java 添加 StructuredServiceItem 提升商家可见度

java

使用 Java 向 Google 我的商家地址添加 StructuredServiceItem

引言

在 Google 上拥有完善的商家资料至关重要,它可以提升企业在搜索和地图等 Google 产品中的可见度和信誉。本文将指导你如何使用 Java 为 Google 我的商家地址添加 StructuredServiceItem。

什么是 StructuredServiceItem?

StructuredServiceItem 是 Google 我的商家中的一种服务类型,允许你提供有关你所提供服务的详细信息,包括服务类型、和其他属性。这有助于为客户提供更全面的服务信息,并提高你的商家资料的可搜索性。

实现 StructuredServiceItem

要使用 Java 添加 StructuredServiceItem,你需要使用 Google My Business API。以下是如何进行操作:

步骤 1:获取访问令牌

首先,你需要通过 OAuth 2.0 授权流程获取 Google My Business API 的访问令牌。这将使你的应用程序能够代表商家访问 Google My Business API。

步骤 2:创建 JSON 更新

接下来,你需要创建一个 JSON 更新对象来包含 StructuredServiceItem。StructuredServiceItem 的 JSON 结构如下:

{
  "serviceTypeId": "BOOL",
  "description": "Service Description"
}

步骤 3:构造 HTTP 请求

使用 JSON 更新对象,你可以构造一个 HTTP PATCH 请求来更新商家资料。请求 URL 应如下所示:

https://mybusinessbusinessinformation.googleapis.com/v1/{locationName}?updateMask={updateMask}&validateOnly={validateOnly}

其中:

  • {locationName} 是商家地址的名称
  • {updateMask} 指定要更新的属性字段
  • {validateOnly} 控制是否仅验证请求而不对商家资料进行任何更改

步骤 4:执行请求

最后,执行 HTTP PATCH 请求。如果请求成功,你将收到 HTTP 200 OK 响应。

代码示例

以下 Java 代码示例展示了如何创建 StructuredServiceItem:

import com.google.gson.JsonObject;
import java.io.IOException;

public class AddStructuredServiceItem {

  public static void main(String[] args) throws IOException {
    // 获取访问令牌
    String accessToken = getAccessToken();

    // 创建 JSON 更新
    JsonObject jsonUpdate = new JsonObject();
    JsonArray serviceItems = new JsonArray();
    String description = "Service Description";
    serviceItems.add(createStructuredServiceItem("BOOL", description));
    jsonUpdate.add("serviceItems", serviceItems);

    // 构造 HTTP 请求
    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 " + accessToken)
        .build();

    // 执行请求
    Response response = client.newCall(request).execute();

    // 处理响应
    if (response.isSuccessful()) {
      System.out.println("Successfully added StructuredServiceItem.");
    } else {
      System.out.println("Failed to add StructuredServiceItem.");
      System.out.println(response.body().string());
    }
  }

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

    return structuredServiceItem;
  }
}

常见问题解答

1. 我可以添加多种类型的 StructuredServiceItem 吗?
是的,你可以添加多种类型的 StructuredServiceItem。

2. 适用于 StructuredServiceItem 的 serviceTypeId 值是什么?
serviceTypeId 值可以是 BOOL、URL、REPEATED_ENUM 或 ENUM。

3. description 字段的长度限制是多少?
description 字段的最大长度为 1000 个字符。

4. 如何验证我的请求是否成功?
如果你收到 HTTP 200 OK 响应,则表示你的请求成功。

5. 如果我更新了 StructuredServiceItem,是否需要重新验证我的商家资料?
是的,每次更改商家资料时,都应重新验证。