借助 Merchant API 测试账号功能,您可以在安全且隔离的空间中全面测试集成,然后再将其部署到正式环境中。通过使用沙盒测试账号,您可以在开发周期的早期阶段尝试 API 调用、验证代码并发现潜在问题,而不会影响生产数据、中断实时操作或无意中违反 Merchant Center 政策。
前提条件
在创建和使用测试账号之前,请确保满足以下要求:
- Merchant Center 账号:您必须拥有 Merchant Center 账号。
- Merchant API 访问权限:您必须是 Merchant API 的注册用户。
测试账号的优势
使用测试账号具有以下几项关键优势:
- 设置简单:测试账号设置流程简单易懂,可让您快速开始测试功能和集成。
- 数据完整性和安全性:生产数据会受到保护,并且消除了生产账号中违反政策的风险。
- 测试效率:您可以测试各种不同的场景和极端情况,而无需为了测试而维护并行的生产账号。
- 立即验证优惠:利用测试账号自动免除首页声明和验证的优势,快速测试优惠插入。对于测试账号,系统默认批准优惠。
- 真实模拟:该环境可模拟产品上传和产品目录管理系统等关键功能的生产行为,确保测试结果可靠。
- 更顺畅的 API 迁移:测试账号支持并行验证,可帮助用户从 Content API 迁移到 Merchant API 或从一个 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。
如果调用成功,则会返回一个 Account 资源,其中包含新测试账号的唯一 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"
}'
限制
测试账号专为功能验证而设计,并具有以下特定限制:
- 测试账号数量:每个 Google 账号最多可创建 5 个测试账号。测试账号会占用每个 Google 账号的 Merchant Center 账号默认限额。
- 与 Merchant API 配额集成:从 API 配额的角度来看,Merchant API 将测试账号视为生产账号。测试账号的配额与正式版账号相同。无法增加测试账号的配额。
- 高级账号:您无法创建高级测试账号,也无法将高级账号转换为测试账号。
- 不会公开投放:提交到测试账号的数据绝无法发布到任何 Google 平台(例如 Google 搜索或购物广告)。
- 受限端点:使用某些功能时,您无法使用测试账号,例如:
- 关联限制:您无法将测试账号与其他 Google Ads 账号或 Google 商家资料账号相关联。
- 测试账号无需注册:您无法为测试账号执行注册。
最佳做法
我们建议您在使用测试账号时遵循一些最佳实践:
- 沙盒优先开发:在将新的集成功能应用于生产环境之前,请务必先在测试账号中验证这些功能。
- 自动化集成测试:使用测试账号作为稳定环境来运行自动化回归测试。
- 测试账号命名:使用
account_name来提示每个测试账号的用途,例如“迁移测试”或“集成测试账号”。