管理和过渡 Google Cloud 项目

如需使用 Merchant API,您必须通过开发者注册将 Google Cloud 项目与您的主要 Merchant Center 账号相关联。在集成的整个生命周期内,您可能需要更改与 Merchant Center 账号关联的 Google Cloud 项目 ID、跨环境管理多个项目,或者在从 Content API for Shopping 迁移时转换项目。

本指南介绍了如何完成以下操作:

  • 在不中断服务的情况下,更改或轮换现有 Merchant API 集成的 Google Cloud 项目 ID。
  • 从一个 Merchant Center 账号中取消注册 Google Cloud 项目,并将其注册到另一个 Merchant Center 账号。
  • 如果您要从 Content API for Shopping 迁移到 Merchant API,请选择并执行正确的 Google Cloud 项目转换策略。

前提条件

在管理或转换 Google Cloud 项目之前,请验证您是否满足开发者注册前提条件。如需了解详情,请参阅 注册为开发者 指南的 前提条件部分。

主要规则和限制

在规划对 Google Cloud 项目的更改时,请注意以下规则:

  • 一对多项目注册: 一个主要 Merchant Center 账号可以同时注册多个 Google Cloud 项目。但是,每个 Google Cloud 项目在任何给定时间都只能注册到一个主要 Merchant Center 账号。如果您尝试注册已关联到另一个 Merchant Center 账号的项目,系统会返回 ALREADY_EXISTS(409 Conflict)错误,并显示原因 DUPLICATE_PROJECT_REGISTRATION。
  • 项目所有权隐私: 出于安全和隐私方面的考虑,Google 购物和 Merchant API 支持团队无法透露 Google Cloud 项目的所有者或管理员电子邮件地址。如果您无法访问某个项目,请与 Google Cloud 支持团队联系。
  • Content API for Shopping 停用延期是特定于项目的: 如果您获得了 Content API for Shopping 的延期访问权限批准,则该延期仅适用于您在请求中提交的特定 Google Cloud 项目 ID。延期不会自动转移到新的 Google Cloud 项目。

更改 Merchant API 账号的 Google Cloud 项目 ID

如果您已在使用 Merchant API,并且需要将集成切换到新的 Google Cloud 项目(例如,由于组织结构调整、凭据轮换政策或无法再以管理员身份访问旧项目),则可以转换项目,而不会中断服务。由于 Merchant Center 账号同时支持多个已注册的 Google Cloud 项目,因此您可以在停用旧项目之前注册新项目。

第 1 步:创建并配置新的 Google Cloud 项目

  1. 在 Google Cloud 控制台中创建一个新项目。 如需了解详细说明,请参阅 创建 Google Cloud 项目。
  2. 在新项目中启用 Merchant API (merchantapi.googleapis.com)。请参阅 启用 Merchant API 部分,该部分位于 创建 Google Cloud 项目中。
  3. 在新 Google Cloud 项目中创建身份验证凭据(服务账号或 OAuth 2.0 客户端 ID)。
  4. 在您的主要 Merchant Center 账号中,将身份验证电子邮件地址(例如新的服务账号电子邮件地址)添加为具有 ADMIN 角色的用户。

第 2 步:注册新的 Google Cloud 项目

使用新 Google Cloud 项目中的凭据进行身份验证 ,然后调用 accounts.developerRegistration.registerGcp 方法,将新项目与您的主要 Merchant Center 账号相关联。如需了解 详细说明,请参阅 注册为开发者中的 发出注册调用 部分。

第 3 步:验证已注册的 Google Cloud 项目

如需确认现有 Google Cloud 项目和新 Google Cloud 项目都已 关联到您的 Merchant Center 账号,请调用 accounts.developerRegistration.getDeveloperRegistration 方法:

GET https://merchantapi.googleapis.com/accounts/v1/accounts/ACCOUNT_ID/developerRegistration
Authorization: Bearer ACCESS_TOKEN

替换以下内容:

  • ACCOUNT_ID:您的 Merchant Center ID
  • ACCESS_TOKEN:用于发出 API 调用的授权令牌

响应会返回 DeveloperRegistration 资源,并在 gcpIds 字段中包含所有关联 云项目的 Google Cloud 项目编号:

{
  "name": "accounts/ACCOUNT_ID/developerRegistration",
  "gcpIds": [
    "123456789012",
    "987654321098"
  ]
}

您还可以通过调用 accounts.developerRegistration.getAccountForGcpRegistration 方法,验证调用 Google Cloud 项目注册到的 Merchant Center 账号:

GET https://merchantapi.googleapis.com/accounts/v1/accounts:getAccountForGcpRegistration
Authorization: Bearer ACCESS_TOKEN

替换以下内容:

  • ACCESS_TOKEN:使用您要检查的 Google Cloud 云项目中的凭据生成的授权令牌

第 4 步:切换流量并取消注册旧 Google Cloud 项目

  1. 更新应用配置,以使用新 Google Cloud 项目中的凭据。由于这两个项目都已注册,因此您的工作负载可以无中断地转换。
  2. 所有流量都使用新项目后,您可以选择使用旧 Google Cloud 项目凭据进行身份验证,然后调用 accounts.developerRegistration.unregisterGcp 方法,移除旧 项目关联:
POST https://merchantapi.googleapis.com/accounts/v1/accounts/ACCOUNT_ID/developerRegistration:unregisterGcp
Content-Type: application/json
Authorization: Bearer OLD_PROJECT_ACCESS_TOKEN

替换以下内容:

  • ACCOUNT_ID:您的 Merchant Center ID
  • OLD_PROJECT_ACCESS_TOKEN:旧 Google Cloud 项目中用于发出 API 调用的授权令牌

Java

import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.shopping.merchant.accounts.v1.DeveloperRegistrationName;
import com.google.shopping.merchant.accounts.v1.DeveloperRegistrationServiceClient;
import com.google.shopping.merchant.accounts.v1.DeveloperRegistrationServiceSettings;
import com.google.shopping.merchant.accounts.v1.UnregisterGcpRequest;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;

/**
 * This class demonstrates how to unregister the GCP project currently used to call the Merchant API
 * for a specific account.
 */
public class UnregisterGcpSample {

  public static void unregisterGcp(Config config) throws Exception {

    // Obtains OAuth token based on the user's configuration.
    GoogleCredentials credential = new Authenticator().authenticate();

    // Creates service settings using the credentials retrieved above.
    DeveloperRegistrationServiceSettings developerRegistrationServiceSettings =
        DeveloperRegistrationServiceSettings.newBuilder()
            .setCredentialsProvider(FixedCredentialsProvider.create(credential))
            .build();

    // Creates DeveloperRegistration name to identify the DeveloperRegistration.
    // The name has the format: accounts/{account}/developerRegistration
    String name =
        DeveloperRegistrationName.newBuilder()
            .setAccount(config.getAccountId().toString())
            .build()
            .toString();

    // Calls the API and propagates any network failures/errors.
    try (DeveloperRegistrationServiceClient developerRegistrationServiceClient =
        DeveloperRegistrationServiceClient.create(developerRegistrationServiceSettings)) {

      // Creates a request to unregister the GCP project.
      UnregisterGcpRequest request = UnregisterGcpRequest.newBuilder().setName(name).build();

      System.out.println("Sending UnregisterGcp request for: " + name);
      developerRegistrationServiceClient.unregisterGcp(request);
      System.out.println("Unregister GCP successful.");
    }
  }

  public static void main(String[] args) throws Exception {
    Config config = Config.load();
    unregisterGcp(config);
  }
}

cURL

curl -X POST \
  "https://merchantapi.googleapis.com/accounts/v1/accounts/ACCOUNT_ID/developerRegistration:unregisterGcp" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer OLD_PROJECT_ACCESS_TOKEN"

将已注册的 Google Cloud 项目重新分配给其他 Merchant Center 账号

由于一个 Google Cloud 项目一次只能注册到一个主要 Merchant Center 账号,因此请按照以下步骤将现有项目从一个 Merchant Center 账号 (OLD_ACCOUNT_ID) 重新分配给 另一个账号 (NEW_ACCOUNT_ID)。如需了解有关选择要注册项目的 Merchant Center 账号的指南,请参阅 重要注意事项:

  1. 使用有权访问 OLD_ACCOUNT_ID 的 Google Cloud 项目中的凭据进行身份验证,然后在 accounts/OLD_ACCOUNT_ID/developerRegistration 上调用unregisterGcp。
  2. 验证身份验证身份在 NEW_ACCOUNT_ID 上是否具有 ADMIN 角色,以及 NEW_ACCOUNT_ID 是否具有已验证的网站。
  3. 在 accounts/NEW_ACCOUNT_ID/developerRegistration 上调用 registerGcp ,使用来自 同一 Google Cloud 项目的凭据。如果 NEW_ACCOUNT_ID 还没有具有 API_DEVELOPER 角色的用户,请在 developerEmail 请求字段中传递有效的用户电子邮件地址。以 .gserviceaccount.com 结尾的服务账号电子邮件地址不能用作 developerEmail。

从 Content API for Shopping 迁移时转换 Google Cloud 项目

从 Content API for Shopping 迁移到 Merchant API 时,您可能会遇到对旧版 Google Cloud 云项目的访问限制,例如,如果您的原始 API 密钥是直接通过旧版 Merchant Center 身份验证工作流配置的,或者原始 Project Owner 已离开您的组织。

根据您是想保留现有项目,还是想在旧项目中保留 Content API for Shopping 的同时,为 Merchant API 使用新项目,请选择以下两个选项之一。

选择迁移项目策略

场景 是否需要对旧项目的管理员访问权限? 要启用的 API 是否需要在新项目中使用 Content API for Shopping 延期?
选项 1:保留现有项目 是(或通过 Google Cloud 支持团队恢复) 在现有 项目中启用 Merchant API(Content API for Shopping 已启用) 否(旧项目 ID 上的现有延期仍然有效)
选项 2:保留旧项目以用于 Content API for Shopping,并使用新项目以用于 Merchant API 否 在新 项目中启用 Merchant API(在旧 项目中保留已启用的 Content API for Shopping) 否 (旧项目保留其延期;新项目仅调用 Merchant API)

选项 1:保留现有 Google Cloud 项目

如果您想将现有 Google Cloud 项目同时用于 Content API for Shopping 和 Merchant API,请选择此选项。

开始之前,请验证或恢复对现有项目的访问权限 。登录 到 Google Cloud 控制台 ,并确认您的账号在 现有项目上拥有以下 任何 角色:

  • 所有者 (roles/owner),
  • Editor (roles/editor)
  • Service Usage Admin (roles/serviceusage.serviceUsageAdmin)
  1. 启用 Merchant API: 在 Google Cloud API 库 中或使用 gcloud 命令启用 merchantapi.googleapis.com。如需了解分步说明,请参阅 通过 Merchant Center 配置 API 身份验证。
  2. 向 Merchant Center 注册 Google Cloud 项目: 按照 注册为开发者 指南中的步骤授权来自现有项目的请求,然后调用 registerGcp 方法,将现有 Google Cloud 项目与您的主要 Merchant Center 账号相关联。您现有的服务账号密钥和已批准的 Content API for Shopping 延期将继续正常运行,无需更改。

选项 2:使用旧 Google Cloud 云项目以用于 Content API for Shopping,并使用新项目以用于 Merchant API

如果您的旧版 Content API for Shopping 集成已在旧 Google Cloud 项目上使用有效凭据和已批准的延期运行,并且您想在单独的专用 Google Cloud 项目中构建新的 Merchant API 集成,请选择此选项:

  1. 在旧项目中保留 Content API for Shopping 集成: 让现有的 Content API for Shopping 工作流和凭据在旧 Google Cloud 项目上运行。如果您针对旧项目 ID 的延期请求已获批准,则您当前的 Content API for Shopping 集成将继续在延期截止日期之前正常运行。
  2. 专门为 Merchant API 创建新的 Google Cloud 云项目: 创建新的 Google Cloud 云项目,启用 merchantapi.googleapis.com,并为新的 Merchant API 工作负载创建凭据(服务账号或 OAuth 2.0 客户端 ID)。
  3. 注册新项目: 按照 注册为开发者中的步骤授权来自新项目的请求,然后使用有效的开发者联系电子邮件地址调用 registerGcp 方法。以 .gserviceaccount.com 结尾的服务账号电子邮件地址不能用作 developerEmail。
  4. 逐步迁移工作负载: 在 Content API for Shopping 计划停用日期 之前(或在批准的延期到期之前),将 API 调用从旧 Content API for Shopping 项目逐步转移到新 Merchant API 项目,并在 迁移完成后停用旧项目。

特殊情况:在完成迁移之前无法再访问旧 Google Cloud 项目

由于 Content API for Shopping 已被废弃,因此它不再列在 Google Cloud API 库中,并且无法在新创建的 Google Cloud 项目中启用 。因此,您无法自行将现有的 Content API for Shopping 工作负载移至新的 Google Cloud 项目。

如果您无法再从旧项目发出 Content API for Shopping 调用(例如,由于凭据被撤消或无法恢复项目访问权限):

  1. 请与 Google Cloud 支持团队 联系,以恢复对现有 Google 云项目的访问权限 (选项 1),或者
  2. 按照选项 2创建并注册新的 Google Cloud 云项目,然后将受影响的工作流直接 迁移到 Merchant API。如果您遇到严重的迁移障碍, 请与 Merchant API 支持团队联系。

后续步骤