Merchant API 中的測試帳戶

Merchant API 測試帳戶功能提供安全且獨立的空間,可讓您在正式環境中部署整合項目之前,先進行全面測試。使用沙箱測試帳戶,您可以在開發週期初期,實驗 API 呼叫、驗證程式碼,以及找出潛在問題,而不會影響實際工作環境資料、中斷即時作業,或不小心違反 Merchant Center 政策。

必要條件

建立及使用測試帳戶前,請確認您符合下列規定:

  • Merchant Center 帳戶:必須擁有 Merchant Center 帳戶。
  • Merchant API 存取權:您必須是 Merchant API 的已註冊的使用者。

測試帳戶的優點

使用測試帳戶有幾項主要優點:

  • 設定簡單:測試帳戶設定程序並不複雜,可讓您快速開始測試功能和整合。
  • 資料完整性和安全性:正式環境資料仍受到保護,且正式環境帳戶不會有違反政策的風險。
  • 測試效率:您可以測試各種情境和極端案例,不必為了測試而維護平行正式版帳戶。
  • 立即驗證商品:測試帳戶可自動豁免首頁聲明和驗證,方便您快速測試商品插入。測試帳戶的優惠預設為核准狀態。
  • 逼真模擬:這個環境會模擬正式環境中產品上傳和商品目錄管理等重要功能的行為,確保測試結果可靠。
  • 更順暢的 API 遷移:測試帳戶可讓您並排驗證,從 Content API 遷移至 Merchant API,或從某個 API 版本遷移至另一個版本時,能更安心且順暢地完成轉換。

如何建立測試帳戶

測試帳戶是使用 Merchant API 中的特定自訂方法建立。

使用 accounts.createTestAccount 方法:

  POST https://merchantapi.googleapis.com/accounts/v1/accounts/{ACCOUNT_ID}:createTestAccount
  Content-Type: application/json
  Authorization: Bearer {ACCESS_TOKEN}"

  {
    "account_name": "{TEST_ACCOUNT_NAME}",
    "time_zone": {
      "id": "America/Los_Angeles"
    },
    "language_code": "en-US"
  }

更改下列內容:

  • ACCOUNT_ID:Merchant Center ID。
  • ACCESS_TOKEN:發出 API 呼叫的授權權杖
  • TEST_ACCOUNT_NAME:測試帳戶的名稱。建議您使用有意義的名稱,表明這些名稱是用於測試。例如,在測試帳戶名稱中加入 test。

建立測試帳戶時,必須填寫下列欄位:

  • time_zone:帳戶的報表和顯示時區。
  • language_code:帳戶的 BCP-47 語言代碼,例如 en-US。

呼叫成功後,系統會傳回帳戶資源,其中包含新測試帳戶的不重複 accountId 和資源名稱:

  {
    "name": "accounts/{TEST_ACCOUNT_ID}",
    "accountId": "{TEST_ACCOUNT_ID}",
    "accountName": "{TEST_ACCOUNT_NAME}",
    "adultContent": false,
    "testAccount": true,
    "timeZone": {
      "id": "America/Los_Angeles"
    },
    "languageCode": "en-US"
  }

下列程式碼範例說明如何建立測試帳戶:

Java

import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.shopping.merchant.accounts.v1.Account;
import com.google.shopping.merchant.accounts.v1.AccountsServiceClient;
import com.google.shopping.merchant.accounts.v1.AccountsServiceSettings;
import com.google.shopping.merchant.accounts.v1.CreateTestAccountRequest;
import com.google.type.TimeZone;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;

/**
 * This class demonstrates how to create a new Merchant Center test account.
 *
 * <p>For more information refer to:
 * https://developers.google.com/merchant/api/guides/accounts/test-accounts
 */
public class CreateTestAccountSample {

  // Method to create a test account.
  public static void createTestAccount(Config config, String newAccountName) throws Exception {

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

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

    // Calls the API and catches and prints any network failures/errors.
    try (AccountsServiceClient accountsServiceClient =
        AccountsServiceClient.create(accountsServiceSettings)) {

      // The test account to be created.
      Account account =
          Account.newBuilder()
              .setAccountName(newAccountName)
              .setTimeZone(TimeZone.newBuilder().setId("Europe/Zurich"))
              .setLanguageCode("en-US")
              .build();

      // Creates parent to identify where to insert the account.
      String parent = String.format("accounts/%s", config.getAccountId());

      // Create the request message.
      CreateTestAccountRequest request =
          CreateTestAccountRequest.newBuilder().setParent(parent).setAccount(account).build();

      System.out.println("Sending Create Test Account request:");
      Account response = accountsServiceClient.createTestAccount(request);

      System.out.println("Created Test Account below:");
      System.out.println(response);
    } catch (Exception e) {
      System.err.println("Error during test account creation:");
      e.printStackTrace();
    }
  }

  // Main method to run the sample.
  public static void main(String[] args) throws Exception {
    Config config = Config.load();

    // This is the name of the new test account to be created.
    String newAccountName = "MyNewTestShop";

    createTestAccount(config, newAccountName);
  }
}

cURL

curl -X POST \
"https://merchantapi.googleapis.com/accounts/v1/accounts/{ACCOUNT_ID}/:createTestAccount" \
-H "Authorization: Bearer {ACCESS_TOKEN}" \
-H "Content-Type: application/json" \
-d '{
 "account_name": "{TEST_ACCOUNT_NAME}",
 "time_zone": {
   "id": "America/Los_Angeles"
   },
 "language_code": "en-US"
}'

進階測試帳戶和子帳戶

accounts.createTestAccount 方法一律會建立獨立測試帳戶。如要設定進階帳戶階層進行測試,請按照下列步驟操作:

  1. 使用 accounts.createTestAccount 建立獨立測試帳戶。
  2. 請按照說明中心文章「轉換為進階帳戶設定」的指引,在 Merchant Center 中將獨立測試帳戶轉換為進階帳戶。
  3. 呼叫 accounts.createAndConfigure 並指定進階測試帳戶為 accountAggregation 供應商,在進階測試帳戶下建立其他測試子帳戶。在測試供應商帳戶下建立的任何子帳戶,都會自動建立為測試帳戶 (testAccount: true)。

限制

測試帳戶用於功能驗證,並有特定限制:

  • 測試帳戶數量:每個 Google 帳戶最多可建立 5 個測試帳戶。測試帳戶會計入每個 Google 帳戶的 Merchant Center 帳戶預設上限。
  • 與 Merchant API 配額整合:從 API 配額的角度來看,Merchant API 會將測試帳戶視為正式版帳戶。測試帳戶的配額與正式版帳戶相同。測試帳戶無法提高配額。
  • 帳戶轉換和進階帳戶:無論是獨立帳戶或進階帳戶,都無法將一般 (正式) 帳戶轉換為測試帳戶,也無法將測試帳戶轉換為正式帳戶。此外,您無法直接使用 accounts.createTestAccount 建立進階測試帳戶。請先建立獨立測試帳戶,再轉換為進階帳戶。
  • 不公開放送:提交至測試帳戶的資料絕不會發布到任何 Google 平台,例如 Google 搜尋或購物廣告。
  • 受限端點:使用特定功能時,無法使用測試帳戶,例如:
  • 連結限制:測試帳戶無法連結至其他 Google Ads 或 Google 商家檔案帳戶。
  • 無法註冊測試帳戶:您無法註冊測試帳戶。

最佳做法

使用測試帳戶時,建議您遵循下列最佳做法:

  • 沙箱優先開發:請一律先在測試帳戶中驗證新的整合功能,再套用至正式環境。
  • 自動化整合測試:將測試帳戶做為穩定環境,執行自動化迴歸測試。
  • 測試帳戶命名:使用 account_name 提示每個測試帳戶的用途,例如「遷移測試」或「整合測試帳戶」。