检索应用服务条款协议状态
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
用于检索应用服务条款协议状态的 Merchant API 代码示例。
Java
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package shopping.merchant.samples.accounts.termsofservices.v1;
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.shopping.merchant.accounts.v1.RetrieveForApplicationTermsOfServiceAgreementStateRequest;
import com.google.shopping.merchant.accounts.v1.TermsOfServiceAgreementState;
import com.google.shopping.merchant.accounts.v1.TermsOfServiceAgreementStateServiceClient;
import com.google.shopping.merchant.accounts.v1.TermsOfServiceAgreementStateServiceSettings;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;
/**
* This class demonstrates how to retrieve the latest TermsOfService agreement state for the
* account.
*/
public class RetrieveForApplicationTermsOfServiceAgreementStateSample {
private static String getParent(String accountId) {
return String.format("accounts/%s", accountId);
}
public static void retrieveForApplicationTermsOfServiceAgreementState(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 parent to identify where to retrieve the application terms of service agreement
// state.
String parent = getParent(config.getAccountId().toString());
// Calls the API and catches and prints any network failures/errors.
try (TermsOfServiceAgreementStateServiceClient termsOfServiceAgreementStateServiceClient =
TermsOfServiceAgreementStateServiceClient.create(
termsOfServiceAgreementStateServiceSettings)) {
// The parent has the format: accounts/{account}
RetrieveForApplicationTermsOfServiceAgreementStateRequest request =
RetrieveForApplicationTermsOfServiceAgreementStateRequest.newBuilder()
.setParent(parent)
.build();
System.out.println("Sending RetrieveForApplication TermsOfService Agreement request:");
TermsOfServiceAgreementState response =
termsOfServiceAgreementStateServiceClient
.retrieveForApplicationTermsOfServiceAgreementState(request);
System.out.println("Retrieved TermsOfServiceAgreementState below");
System.out.println(response);
} catch (Exception e) {
System.out.println(e);
}
}
public static void main(String[] args) throws Exception {
Config config = Config.load();
retrieveForApplicationTermsOfServiceAgreementState(config);
}
}
PHP
<?php
/**
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
require_once __DIR__ . '/../../../../vendor/autoload.php';
require_once __DIR__ . '/../../../Authentication/Authentication.php';
require_once __DIR__ . '/../../../Authentication/Config.php';
use Google\ApiCore\ApiException;
use Google\Shopping\Merchant\Accounts\V1\Client\TermsOfServiceAgreementStateServiceClient;
use Google\Shopping\Merchant\Accounts\V1\RetrieveForApplicationTermsOfServiceAgreementStateRequest;
/**
* Demonstrates how to retrieve the latest TermsOfService agreement state for the account.
*/
class RetrieveForApplicationTermsOfServiceAgreementState
{
private static function getParent($accountId)
{
return sprintf("accounts/%s", $accountId);
}
/**
* Retrieves the latest TermsOfService agreement state for the account.
*
* @param array $config The configuration data.
* @return void
*/
public static function retrieveForApplicationTermsOfServiceAgreementState($config): void
{
// Get OAuth credentials.
$credentials = Authentication::useServiceAccountOrTokenFile();
// Create client options.
$options = ['credentials' => $credentials];
// Create a TermsOfServiceAgreementStateServiceClient.
$termsOfServiceAgreementStateServiceClient = new TermsOfServiceAgreementStateServiceClient($options);
// Create parent.
$parent = self::getParent($config['accountId']);
try {
// Prepare the request.
$request = new RetrieveForApplicationTermsOfServiceAgreementStateRequest([
'parent' => $parent,
]);
print "Sending RetrieveForApplication TermsOfService Agreement request:" . PHP_EOL;
$response = $termsOfServiceAgreementStateServiceClient->retrieveForApplicationTermsOfServiceAgreementState($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::retrieveForApplicationTermsOfServiceAgreementState($config);
}
}
// Run the script
$sample = new RetrieveForApplicationTermsOfServiceAgreementState();
$sample->callSample();
Python
# -*- coding: utf-8 -*-
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Module for retrieving the latest Merchant Center account's TermsOfService state."""
from examples.authentication import configuration
from examples.authentication import generate_user_credentials
from google.shopping.merchant_accounts_v1 import RetrieveForApplicationTermsOfServiceAgreementStateRequest
from google.shopping.merchant_accounts_v1 import TermsOfServiceAgreementStateServiceClient
# Replace with your actual value.
_ACCOUNT_ID = configuration.Configuration().read_merchant_info()
def retrieve_for_application_terms_of_service_agreement_state():
"""Retrieves the latest TermsOfService agreement state for the account."""
credentials = generate_user_credentials.main()
client = TermsOfServiceAgreementStateServiceClient(credentials=credentials)
parent = f"accounts/{_ACCOUNT_ID}"
request = RetrieveForApplicationTermsOfServiceAgreementStateRequest(
parent=parent
)
try:
print("Sending RetrieveForApplication TermsOfService Agreement request:")
response = client.retrieve_for_application_terms_of_service_agreement_state(
request=request
)
print("Retrieved TermsOfServiceAgreementState below")
print(response)
except RuntimeError as e:
print(e)
if __name__ == "__main__":
retrieve_for_application_terms_of_service_agreement_state()
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-21。
[null,null,["最后更新时间 (UTC):2025-08-21。"],[[["\u003cp\u003eThis code demonstrates how to retrieve the latest Terms of Service agreement state for a Merchant Center account using the Merchant API.\u003c/p\u003e\n"],["\u003cp\u003eThe code samples are provided in Java, PHP, and Python, showcasing the implementation across multiple programming languages.\u003c/p\u003e\n"],["\u003cp\u003eEach sample utilizes the \u003ccode\u003eTermsOfServiceAgreementStateServiceClient\u003c/code\u003e to send a \u003ccode\u003eRetrieveForApplicationTermsOfServiceAgreementStateRequest\u003c/code\u003e to the Merchant API, specifying the parent account.\u003c/p\u003e\n"],["\u003cp\u003eThe code shows how to handle API requests, authentication, and responses, including error handling for network failures and API exceptions.\u003c/p\u003e\n"],["\u003cp\u003eThe code will return the response of the latest terms of service agreement in the format of a JSON string.\u003c/p\u003e\n"]]],["The provided code samples demonstrate retrieving the latest Terms of Service (ToS) agreement state for a Merchant Center account. Key actions include: authenticating with OAuth credentials, creating a `TermsOfServiceAgreementStateServiceClient`, constructing a `RetrieveForApplicationTermsOfServiceAgreementStateRequest` with the account ID, and sending the request to the API. The response, containing the ToS agreement state, is then printed, or the code will catch and print any network failure. These steps are implemented across Java, PHP, and Python.\n"],null,["# Retrieve for application terms of service agreement state\n\nMerchant API code sample to retrieve for application terms of service agreement\nstate. \n\n### Java\n\n // Copyright 2024 Google LLC\n //\n // Licensed under the Apache License, Version 2.0 (the \"License\");\n // you may not use this file except in compliance with the License.\n // You may obtain a copy of the License at\n //\n // https://www.apache.org/licenses/LICENSE-2.0\n //\n // Unless required by applicable law or agreed to in writing, software\n // distributed under the License is distributed on an \"AS IS\" BASIS,\n // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n // See the License for the specific language governing permissions and\n // limitations under the License.\n\n package shopping.merchant.samples.accounts.termsofservices.v1;\n import com.google.api.gax.core.FixedCredentialsProvider;\n import com.google.auth.oauth2.GoogleCredentials;\n import com.google.shopping.merchant.accounts.v1.RetrieveForApplicationTermsOfServiceAgreementStateRequest;\n import com.google.shopping.merchant.accounts.v1.TermsOfServiceAgreementState;\n import com.google.shopping.merchant.accounts.v1.TermsOfServiceAgreementStateServiceClient;\n import com.google.shopping.merchant.accounts.v1.TermsOfServiceAgreementStateServiceSettings;\n import shopping.merchant.samples.utils.Authenticator;\n import shopping.merchant.samples.utils.Config;\n\n /**\n * This class demonstrates how to retrieve the latest TermsOfService agreement state for the\n * account.\n */\n public class RetrieveForApplicationTermsOfServiceAgreementStateSample {\n\n private static String getParent(String accountId) {\n return String.format(\"accounts/%s\", accountId);\n }\n\n public static void retrieveForApplicationTermsOfServiceAgreementState(Config config)\n throws Exception {\n\n // Obtains OAuth token based on the user's configuration.\n GoogleCredentials credential = new Authenticator().authenticate();\n\n // Creates service settings using the credentials retrieved above.\n TermsOfServiceAgreementStateServiceSettings termsOfServiceAgreementStateServiceSettings =\n TermsOfServiceAgreementStateServiceSettings.newBuilder()\n .setCredentialsProvider(FixedCredentialsProvider.create(credential))\n .build();\n\n // Creates parent to identify where to retrieve the application terms of service agreement\n // state.\n String parent = getParent(config.getAccountId().toString());\n\n // Calls the API and catches and prints any network failures/errors.\n try (TermsOfServiceAgreementStateServiceClient termsOfServiceAgreementStateServiceClient =\n TermsOfServiceAgreementStateServiceClient.create(\n termsOfServiceAgreementStateServiceSettings)) {\n\n // The parent has the format: accounts/{account}\n RetrieveForApplicationTermsOfServiceAgreementStateRequest request =\n RetrieveForApplicationTermsOfServiceAgreementStateRequest.newBuilder()\n .setParent(parent)\n .build();\n\n System.out.println(\"Sending RetrieveForApplication TermsOfService Agreement request:\");\n TermsOfServiceAgreementState response =\n termsOfServiceAgreementStateServiceClient\n .retrieveForApplicationTermsOfServiceAgreementState(request);\n\n System.out.println(\"Retrieved TermsOfServiceAgreementState below\");\n System.out.println(response);\n } catch (Exception e) {\n System.out.println(e);\n }\n }\n\n public static void main(String[] args) throws Exception {\n Config config = Config.load();\n\n retrieveForApplicationTermsOfServiceAgreementState(config);\n }\n } \n https://github.com/google/merchant-api-samples/blob/c6de994268c785ce22af0065932518a9ac5b3c03/java/src/main/java/shopping/merchant/samples/accounts/termsofservices/v1/RetrieveForApplicationTermsOfServiceAgreementStateSample.java\n\n### PHP\n\n \u003c?php\n /**\n * Copyright 2025 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n require_once __DIR__ . '/../../../../vendor/autoload.php';\n require_once __DIR__ . '/../../../Authentication/Authentication.php';\n require_once __DIR__ . '/../../../Authentication/Config.php';\n use Google\\ApiCore\\ApiException;\n use Google\\Shopping\\Merchant\\Accounts\\V1\\Client\\TermsOfServiceAgreementStateServiceClient;\n use Google\\Shopping\\Merchant\\Accounts\\V1\\RetrieveForApplicationTermsOfServiceAgreementStateRequest;\n\n /**\n * Demonstrates how to retrieve the latest TermsOfService agreement state for the account.\n */\n class RetrieveForApplicationTermsOfServiceAgreementState\n {\n private static function getParent($accountId)\n {\n return sprintf(\"accounts/%s\", $accountId);\n }\n /**\n * Retrieves the latest TermsOfService agreement state for the account.\n *\n * @param array $config The configuration data.\n * @return void\n */\n public static function retrieveForApplicationTermsOfServiceAgreementState($config): void\n {\n // Get OAuth credentials.\n $credentials = Authentication::useServiceAccountOrTokenFile();\n\n // Create client options.\n $options = ['credentials' =\u003e $credentials];\n\n // Create a TermsOfServiceAgreementStateServiceClient.\n $termsOfServiceAgreementStateServiceClient = new TermsOfServiceAgreementStateServiceClient($options);\n\n // Create parent.\n $parent = self::getParent($config['accountId']);\n\n try {\n // Prepare the request.\n $request = new RetrieveForApplicationTermsOfServiceAgreementStateRequest([\n 'parent' =\u003e $parent,\n ]);\n\n print \"Sending RetrieveForApplication TermsOfService Agreement request:\" . PHP_EOL;\n $response = $termsOfServiceAgreementStateServiceClient-\u003eretrieveForApplicationTermsOfServiceAgreementState($request);\n\n print \"Retrieved TermsOfServiceAgreementState below\\n\";\n print $response-\u003eserializeToJsonString() . PHP_EOL;\n } catch (ApiException $e) {\n print $e-\u003egetMessage();\n }\n }\n\n /**\n * Helper to execute the sample.\n *\n * @return void\n */\n public function callSample(): void\n {\n $config = Config::generateConfig();\n self::retrieveForApplicationTermsOfServiceAgreementState($config);\n }\n\n }\n\n // Run the script\n $sample = new RetrieveForApplicationTermsOfServiceAgreementState();\n $sample-\u003ecallSample(); \n https://github.com/google/merchant-api-samples/blob/c6de994268c785ce22af0065932518a9ac5b3c03/php/examples/accounts/termsofservices/v1/RetrieveForApplicationTermsOfServiceAgreementStateSample.php\n\n### Python\n\n # -*- coding: utf-8 -*-\n # Copyright 2024 Google LLC\n #\n # Licensed under the Apache License, Version 2.0 (the \"License\");\n # you may not use this file except in compliance with the License.\n # You may obtain a copy of the License at\n #\n # http://www.apache.org/licenses/LICENSE-2.0\n #\n # Unless required by applicable law or agreed to in writing, software\n # distributed under the License is distributed on an \"AS IS\" BASIS,\n # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n # See the License for the specific language governing permissions and\n # limitations under the License.\n \"\"\"Module for retrieving the latest Merchant Center account's TermsOfService state.\"\"\"\n\n\n from examples.authentication import configuration\n from examples.authentication import generate_user_credentials\n from google.shopping.merchant_accounts_v1 import RetrieveForApplicationTermsOfServiceAgreementStateRequest\n from google.shopping.merchant_accounts_v1 import TermsOfServiceAgreementStateServiceClient\n\n # Replace with your actual value.\n _ACCOUNT_ID = configuration.Configuration().read_merchant_info()\n\n\n def retrieve_for_application_terms_of_service_agreement_state():\n \"\"\"Retrieves the latest TermsOfService agreement state for the account.\"\"\"\n\n credentials = generate_user_credentials.main()\n client = TermsOfServiceAgreementStateServiceClient(credentials=credentials)\n\n parent = f\"accounts/{_ACCOUNT_ID}\"\n\n request = RetrieveForApplicationTermsOfServiceAgreementStateRequest(\n parent=parent\n )\n\n try:\n print(\"Sending RetrieveForApplication TermsOfService Agreement request:\")\n response = client.retrieve_for_application_terms_of_service_agreement_state(\n request=request\n )\n print(\"Retrieved TermsOfServiceAgreementState below\")\n print(response)\n except RuntimeError as e:\n print(e)\n\n\n if __name__ == \"__main__\":\n retrieve_for_application_terms_of_service_agreement_state()\n\n https://github.com/google/merchant-api-samples/blob/c6de994268c785ce22af0065932518a9ac5b3c03/python/examples/accounts/termsofservices/v1/retrieve_for_application_terms_of_service_agreement_state_sample.py"]]