您需要拥有 Merchant Center 账号才能使用 Merchant API。您可以使用 Merchant Center 界面创建此类产品。
如果您需要管理多个账号,可以使用 Merchant API 创建子账号。
您可以在 Merchant Center 界面中或通过 API 配置账号(如后文所述)。
接受 Merchant Center 服务条款
所有商家都必须接受 Merchant Center 服务条款。如需接受您自己商家账号的《服务条款》,请按以下步骤操作:
调用
accounts.termsOfServiceAgreementStates.retrieveForApplication
可了解您的账号需要接受哪些服务条款。以下是请求示例:
GET https://merchantapi.googleapis.com/accounts/v1beta/accounts/{ACCOUNT_ID}/termsOfServiceAgreementStates:retrieveForApplication
以下是成功调用的示例响应:
{ "name": "accounts/{ACCOUNT_ID}/termsOfServiceAgreementStates/MERCHANT_CENTER-{COUNTRY}", "regionCode": {COUNTRY}, "termsOfServiceKind": "MERCHANT_CENTER", "accepted": { "termsOfService": "termsOfService/{VERSION}", "acceptedBy": "accounts/{ACCOUNT_ID}" } }
拨打
termsOfService.accept
接受服务条款。以下是请求示例:
GET https://merchantapi.googleapis.com/accounts/v1beta/{name=termsOfService/{VERSION}}:accept
如果成功,则响应正文将为空。
我们建议您构建一个界面,用于向商家显示服务条款并要求他们接受。
使用商家的
regionCode
与termsOfService.retrieveLatest
一起查找商家需要接受的服务条款。以下是请求示例:
GET https://merchantapi.googleapis.com/accounts/v1beta/termsOfService:retrieveLatest
以下是成功调用的示例响应:
{ "name": "termsOfService/{VERSION}", "regionCode": "{COUNTRY}", "kind": "MERCHANT_CENTER", "fileUri": "{URI}" }
向商家显示
fileUri
中的服务条款。当商家在您的界面中接受服务条款时,请调用
termsOfService.accept
,并提供要接受的服务条款的name
。
您可以使用以下示例为给定账号接受服务条款协议:
Java
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.shopping.merchant.accounts.v1beta.AcceptTermsOfServiceRequest;
import com.google.shopping.merchant.accounts.v1beta.TermsOfServiceServiceClient;
import com.google.shopping.merchant.accounts.v1beta.TermsOfServiceServiceSettings;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;
/** This class demonstrates how to accept the TermsOfService agreement in a given account. */
public class AcceptTermsOfServiceSample {
public static void acceptTermsOfService(String accountId, String tosVersion, String regionCode)
throws Exception {
// Obtains OAuth token based on the user's configuration.
GoogleCredentials credential = new Authenticator().authenticate();
// Creates service settings using the credentials retrieved above.
TermsOfServiceServiceSettings tosServiceSettings =
TermsOfServiceServiceSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(credential))
.build();
// Calls the API and catches and prints any network failures/errors.
try (TermsOfServiceServiceClient tosServiceClient =
TermsOfServiceServiceClient.create(tosServiceSettings)) {
// The parent has the format: accounts/{account}
AcceptTermsOfServiceRequest request =
AcceptTermsOfServiceRequest.newBuilder()
.setName(String.format("termsOfService/%s", tosVersion))
.setAccount(String.format("accounts/%s", accountId))
.setRegionCode(regionCode)
.build();
System.out.println("Sending request to accept terms of service...");
tosServiceClient.acceptTermsOfService(request);
System.out.println("Successfully accepted terms of service.");
} catch (Exception e) {
System.out.println(e);
}
}
public static void main(String[] args) throws Exception {
Config config = Config.load();
// See GetTermsOfServiceAgreementStateSample to understand how to check which version of the
// terms of service needs to be accepted, if any.
// Likewise, if you know that the terms of service needs to be accepted, you can also simply
// call RetrieveLatestTermsOfService to get the latest version of the terms of service.
// Region code is either a country when the ToS applies specifically to that country or 001 when
// it applies globally.
acceptTermsOfService(config.getAccountId().toString(), "VERSION_HERE", "REGION_CODE_HERE");
}
}
PHP
use Google\ApiCore\ApiException;
use Google\Shopping\Merchant\Accounts\V1beta\AcceptTermsOfServiceRequest;
use Google\Shopping\Merchant\Accounts\V1beta\Client\TermsOfServiceServiceClient;
/**
* Demonstrates how to accept the TermsOfService agreement in a given account.
*/
class AcceptTermsOfService
{
/**
* Accepts the Terms of Service agreement.
*
* @param string $accountId The account ID.
* @param string $tosVersion The Terms of Service version.
* @param string $regionCode The region code.
* @return void
*/
public static function acceptTermsOfService($accountId, $tosVersion, $regionCode): void
{
// Get OAuth credentials.
$credentials = Authentication::useServiceAccountOrTokenFile();
// Create client options.
$options = ['credentials' => $credentials];
// Create a TermsOfServiceServiceClient.
$tosServiceClient = new TermsOfServiceServiceClient($options);
try {
// Prepare the request.
$request = new AcceptTermsOfServiceRequest([
'name' => sprintf("termsOfService/%s", $tosVersion),
'account' => sprintf("accounts/%s", $accountId),
'region_code' => $regionCode,
]);
print "Sending request to accept terms of service...\n";
$tosServiceClient->acceptTermsOfService($request);
print "Successfully accepted terms of service.\n";
} catch (ApiException $e) {
print $e->getMessage();
}
}
/**
* Helper to execute the sample.
*
* @return void
*/
public function callSample(): void
{
$config = Config::generateConfig();
// Replace with actual values.
$tosVersion = "132";
$regionCode = "US";
self::acceptTermsOfService($config['accountId'], $tosVersion, $regionCode);
}
}
// Run the script
$sample = new AcceptTermsOfService();
$sample->callSample();
Python
from examples.authentication import configuration
from examples.authentication import generate_user_credentials
from google.shopping.merchant_accounts_v1beta import AcceptTermsOfServiceRequest
from google.shopping.merchant_accounts_v1beta import TermsOfServiceServiceClient
# Replace with your actual values.
_ACCOUNT_ID = configuration.Configuration().read_merchant_info()
_TOS_VERSION = ( # Replace with the Terms of Service version to accept
"VERSION_HERE"
)
_REGION_CODE = "US" # Replace with the region code
def accept_terms_of_service():
"""Accepts the Terms of Service agreement for a given account."""
credentials = generate_user_credentials.main()
client = TermsOfServiceServiceClient(credentials=credentials)
# Construct the request
request = AcceptTermsOfServiceRequest(
name=f"termsOfService/{_TOS_VERSION}",
account=f"accounts/{_ACCOUNT_ID}",
region_code=_REGION_CODE,
)
try:
print("Sending request to accept terms of service...")
client.accept_terms_of_service(request=request)
print("Successfully accepted terms of service.")
except RuntimeError as e:
print(e)
if __name__ == "__main__":
accept_terms_of_service()
如果您想检索特定国家/地区的服务条款协议状态,请使用以下示例:
Java
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.shopping.merchant.accounts.v1beta.GetTermsOfServiceAgreementStateRequest;
import com.google.shopping.merchant.accounts.v1beta.TermsOfServiceAgreementState;
import com.google.shopping.merchant.accounts.v1beta.TermsOfServiceAgreementStateName;
import com.google.shopping.merchant.accounts.v1beta.TermsOfServiceAgreementStateServiceClient;
import com.google.shopping.merchant.accounts.v1beta.TermsOfServiceAgreementStateServiceSettings;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;
/**
* This class demonstrates how to get a TermsOfServiceAgreementState for a specific
* TermsOfServiceKind and country.
*/
public class GetTermsOfServiceAgreementStateSample {
public static void getTermsOfServiceAgreementState(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.
TermsOfServiceAgreementStateServiceSettings termsOfServiceAgreementStateServiceSettings =
TermsOfServiceAgreementStateServiceSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(credential))
.build();
// Creates TermsOfServiceAgreementState name to identify TermsOfServiceAgreementState.
String name =
TermsOfServiceAgreementStateName.newBuilder()
.setAccount(config.getAccountId().toString())
// The Identifier is: "{TermsOfServiceKind}-{country}"
.setIdentifier("MERCHANT_CENTER-US")
.build()
.toString();
System.out.println(name);
// Calls the API and catches and prints any network failures/errors.
try (TermsOfServiceAgreementStateServiceClient termsOfServiceAgreementStateServiceClient =
TermsOfServiceAgreementStateServiceClient.create(
termsOfServiceAgreementStateServiceSettings)) {
// The name has the format:
// accounts/{account}/termsOfServiceAgreementStates/{TermsOfServiceKind}-{country}
GetTermsOfServiceAgreementStateRequest request =
GetTermsOfServiceAgreementStateRequest.newBuilder().setName(name).build();
System.out.println("Sending Get TermsOfServiceAgreementState request:");
TermsOfServiceAgreementState response =
termsOfServiceAgreementStateServiceClient.getTermsOfServiceAgreementState(request);
System.out.println("Retrieved TermsOfServiceAgreementState below");
// If the terms of service needs to be accepted, the "required" field will include the
// specific version of the terms of service which needs to be accepted, alongside a link to
// the terms of service itself.
System.out.println(response);
} catch (Exception e) {
System.out.println(e);
}
}
public static void main(String[] args) throws Exception {
Config config = Config.load();
getTermsOfServiceAgreementState(config);
}
}
PHP
use Google\ApiCore\ApiException;
use Google\Shopping\Merchant\Accounts\V1beta\Client\TermsOfServiceAgreementStateServiceClient;
use Google\Shopping\Merchant\Accounts\V1beta\GetTermsOfServiceAgreementStateRequest;
/**
* Demonstrates how to get a TermsOfServiceAgreementState.
*/
class GetTermsOfServiceAgreementState
{
/**
* Gets a TermsOfServiceAgreementState.
*
* @param array $config The configuration data.
* @return void
*/
public static function getTermsOfServiceAgreementState($config): void
{
// Get OAuth credentials.
$credentials = Authentication::useServiceAccountOrTokenFile();
// Create client options.
$options = ['credentials' => $credentials];
// Create a TermsOfServiceAgreementStateServiceClient.
$termsOfServiceAgreementStateServiceClient = new TermsOfServiceAgreementStateServiceClient($options);
// Service agreeement identifier
$identifier = "MERCHANT_CENTER-US";
// Create TermsOfServiceAgreementState name.
$name = "accounts/" . $config['accountId'] . "/termsOfServiceAgreementStates/" . $identifier;
print $name . PHP_EOL;
try {
// Prepare the request.
$request = new GetTermsOfServiceAgreementStateRequest([
'name' => $name,
]);
print "Sending Get TermsOfServiceAgreementState request:" . PHP_EOL;
$response = $termsOfServiceAgreementStateServiceClient->getTermsOfServiceAgreementState($request);
print "Retrieved TermsOfServiceAgreementState below\n";
print $response->serializeToJsonString() . PHP_EOL;
} catch (ApiException $e) {
print $e->getMessage();
}
}
/**
* Helper to execute the sample.
*
* @return void
*/
public function callSample(): void
{
$config = Config::generateConfig();
self::getTermsOfServiceAgreementState($config);
}
}
// Run the script
$sample = new GetTermsOfServiceAgreementState();
$sample->callSample();
Python
from examples.authentication import configuration
from examples.authentication import generate_user_credentials
from google.shopping.merchant_accounts_v1beta import GetTermsOfServiceAgreementStateRequest
from google.shopping.merchant_accounts_v1beta import TermsOfServiceAgreementStateServiceClient
# Replace with your actual value.
_ACCOUNT_ID = configuration.Configuration().read_merchant_info()
_IDENTIFIER = "MERCHANT_CENTER-US" # Replace with your identifier
def get_terms_of_service_agreement_state():
"""Gets a TermsOfServiceAgreementState for a specific TermsOfServiceKind and country."""
credentials = generate_user_credentials.main()
client = TermsOfServiceAgreementStateServiceClient(credentials=credentials)
name = (
"accounts/"
+ _ACCOUNT_ID
+ "/termsOfServiceAgreementStates/"
+ _IDENTIFIER
)
print(name)
request = GetTermsOfServiceAgreementStateRequest(name=name)
try:
print("Sending Get TermsOfServiceAgreementState request:")
response = client.get_terms_of_service_agreement_state(request=request)
print("Retrieved TermsOfServiceAgreementState below")
print(response)
except RuntimeError as e:
print(e)
if __name__ == "__main__":
get_terms_of_service_agreement_state()
商家接受服务条款后,您可以使用 Merchant API 设置其余的账号信息。如需详细了解您可以使用 Merchant Accounts API 管理哪些账号信息,请参阅 Account
资源。
声明网站所有权
您可以使用 Merchant Accounts API 添加和声明商家的 Homepage
。
- 如需向您的账号添加首页,请调用
accounts.updateHomepage
,并提供包含首页网址的Homepage
资源。 - 如需声明对首页的所有权,请使用
Hompeage
资源中的name
调用accounts.homepage.claim
。
您无法使用 Merchant API 验证您的首页。如需了解详情,请参阅验证您商店的网站并声明其所有权。
更新您的商家详情
您可以使用 Merchant Accounts API 修改商家 PostalAddress
、CusomerService
和 BusinessIdentity
。
商家身份:
- 如需查看您的商家身份,请调用
accounts.businessIdentity.getBusinessIdentity
。 - 如需修改商家身份,请拨打
accounts.businessIdentity.updateBusinessIdentity
。