شرایط خدمات را بپذیرید، شرایط خدمات را بپذیرید
با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
نمونه کد API Merchant برای پذیرش شرایط خدمات.
جاوا
// 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.AcceptTermsOfServiceRequest;
import com.google.shopping.merchant.accounts.v1.TermsOfServiceServiceClient;
import com.google.shopping.merchant.accounts.v1.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
<?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\AcceptTermsOfServiceRequest;
use Google\Shopping\Merchant\Accounts\V1\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();
پایتون
# -*- 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 accepting the Terms of Service agreement for a given account."""
from examples.authentication import configuration
from examples.authentication import generate_user_credentials
from google.shopping.merchant_accounts_v1 import AcceptTermsOfServiceRequest
from google.shopping.merchant_accounts_v1 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()
جز در مواردی که غیر از این ذکر شده باشد،محتوای این صفحه تحت مجوز Creative Commons Attribution 4.0 License است. نمونه کدها نیز دارای مجوز Apache 2.0 License است. برای اطلاع از جزئیات، به خطمشیهای سایت Google Developers مراجعه کنید. جاوا علامت تجاری ثبتشده Oracle و/یا شرکتهای وابسته به آن است.
تاریخ آخرین بهروزرسانی 2025-08-21 بهوقت ساعت هماهنگ جهانی.
[null,null,["تاریخ آخرین بهروزرسانی 2025-08-21 بهوقت ساعت هماهنگ جهانی."],[[["\u003cp\u003eThis webpage provides code samples in Java, PHP, and Python demonstrating how to accept the Terms of Service agreement for a given merchant account using the Merchant API.\u003c/p\u003e\n"],["\u003cp\u003eThe code samples show how to create an \u003ccode\u003eAcceptTermsOfServiceRequest\u003c/code\u003e with the account ID, Terms of Service version, and region code.\u003c/p\u003e\n"],["\u003cp\u003eAuthentication using OAuth credentials is required, and the samples illustrate how to retrieve these credentials to use within the request.\u003c/p\u003e\n"],["\u003cp\u003eThe provided examples are structured to send a request to the \u003ccode\u003eTermsOfServiceServiceClient\u003c/code\u003e to accept the specified terms, handling any potential API or network errors.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eacceptTermsOfService\u003c/code\u003e function is consistent across all three languages, allowing it to accept the terms of service with the given parameters and output the result of the request.\u003c/p\u003e\n"]]],["The code samples demonstrate how to accept the Terms of Service (ToS) for a given account using the Merchant API in Java, PHP, and Python. They first obtain OAuth credentials, then use them to create a `TermsOfServiceServiceClient`. An `AcceptTermsOfServiceRequest` is built, including the account ID, ToS version, and region code. Finally, the `acceptTermsOfService` method is called to send the request, and confirmation is printed upon success. Each sample provides examples for setting parameters.\n"],null,["# Accept terms of service\n\nMerchant API code sample to accept terms of service. \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.AcceptTermsOfServiceRequest;\n import com.google.shopping.merchant.accounts.v1.TermsOfServiceServiceClient;\n import com.google.shopping.merchant.accounts.v1.TermsOfServiceServiceSettings;\n import shopping.merchant.samples.utils.Authenticator;\n import shopping.merchant.samples.utils.Config;\n\n /** This class demonstrates how to accept the TermsOfService agreement in a given account. */\n public class AcceptTermsOfServiceSample {\n\n public static void acceptTermsOfService(String accountId, String tosVersion, String regionCode)\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 TermsOfServiceServiceSettings tosServiceSettings =\n TermsOfServiceServiceSettings.newBuilder()\n .setCredentialsProvider(FixedCredentialsProvider.create(credential))\n .build();\n\n // Calls the API and catches and prints any network failures/errors.\n try (TermsOfServiceServiceClient tosServiceClient =\n TermsOfServiceServiceClient.create(tosServiceSettings)) {\n\n // The parent has the format: accounts/{account}\n AcceptTermsOfServiceRequest request =\n AcceptTermsOfServiceRequest.newBuilder()\n .setName(String.format(\"termsOfService/%s\", tosVersion))\n .setAccount(String.format(\"accounts/%s\", accountId))\n .setRegionCode(regionCode)\n .build();\n\n System.out.println(\"Sending request to accept terms of service...\");\n tosServiceClient.acceptTermsOfService(request);\n\n System.out.println(\"Successfully accepted terms of service.\");\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 // See GetTermsOfServiceAgreementStateSample to understand how to check which version of the\n // terms of service needs to be accepted, if any.\n // Likewise, if you know that the terms of service needs to be accepted, you can also simply\n // call RetrieveLatestTermsOfService to get the latest version of the terms of service.\n // Region code is either a country when the ToS applies specifically to that country or 001 when\n // it applies globally.\n acceptTermsOfService(config.getAccountId().toString(), \"VERSION_HERE\", \"REGION_CODE_HERE\");\n }\n } \n https://github.com/google/merchant-api-samples/blob/c6de994268c785ce22af0065932518a9ac5b3c03/java/src/main/java/shopping/merchant/samples/accounts/termsofservices/v1/AcceptTermsOfServiceSample.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\\AcceptTermsOfServiceRequest;\n use Google\\Shopping\\Merchant\\Accounts\\V1\\Client\\TermsOfServiceServiceClient;\n\n /**\n * Demonstrates how to accept the TermsOfService agreement in a given account.\n */\n class AcceptTermsOfService\n {\n\n /**\n * Accepts the Terms of Service agreement.\n *\n * @param string $accountId The account ID.\n * @param string $tosVersion The Terms of Service version.\n * @param string $regionCode The region code.\n * @return void\n */\n public static function acceptTermsOfService($accountId, $tosVersion, $regionCode): void\n {\n // Get OAuth credentials.\n $credentials = Authentication::useServiceAccountOrTokenFile();\n\n // Create client options.\n $options = ['credentials' =\u003e $credentials];\n\n // Create a TermsOfServiceServiceClient.\n $tosServiceClient = new TermsOfServiceServiceClient($options);\n\n try {\n // Prepare the request.\n $request = new AcceptTermsOfServiceRequest([\n 'name' =\u003e sprintf(\"termsOfService/%s\", $tosVersion),\n 'account' =\u003e sprintf(\"accounts/%s\", $accountId),\n 'region_code' =\u003e $regionCode,\n ]);\n\n print \"Sending request to accept terms of service...\\n\";\n $tosServiceClient-\u003eacceptTermsOfService($request);\n\n print \"Successfully accepted terms of service.\\n\";\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\n // Replace with actual values.\n $tosVersion = \"132\";\n $regionCode = \"US\";\n\n self::acceptTermsOfService($config['accountId'], $tosVersion, $regionCode);\n }\n }\n\n // Run the script\n $sample = new AcceptTermsOfService();\n $sample-\u003ecallSample(); \n https://github.com/google/merchant-api-samples/blob/c6de994268c785ce22af0065932518a9ac5b3c03/php/examples/accounts/termsofservices/v1/AcceptTermsOfServiceSample.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 accepting the Terms of Service agreement for a given account.\"\"\"\n\n from examples.authentication import configuration\n from examples.authentication import generate_user_credentials\n from google.shopping.merchant_accounts_v1 import AcceptTermsOfServiceRequest\n from google.shopping.merchant_accounts_v1 import TermsOfServiceServiceClient\n\n # Replace with your actual values.\n _ACCOUNT_ID = configuration.Configuration().read_merchant_info()\n _TOS_VERSION = ( # Replace with the Terms of Service version to accept\n \"VERSION_HERE\"\n )\n _REGION_CODE = \"US\" # Replace with the region code\n\n\n def accept_terms_of_service():\n \"\"\"Accepts the Terms of Service agreement for a given account.\"\"\"\n\n credentials = generate_user_credentials.main()\n client = TermsOfServiceServiceClient(credentials=credentials)\n\n # Construct the request\n request = AcceptTermsOfServiceRequest(\n name=f\"termsOfService/{_TOS_VERSION}\",\n account=f\"accounts/{_ACCOUNT_ID}\",\n region_code=_REGION_CODE,\n )\n\n try:\n print(\"Sending request to accept terms of service...\")\n client.accept_terms_of_service(request=request)\n print(\"Successfully accepted terms of service.\")\n except RuntimeError as e:\n print(e)\n\n\n if __name__ == \"__main__\":\n accept_terms_of_service()\n\n https://github.com/google/merchant-api-samples/blob/c6de994268c785ce22af0065932518a9ac5b3c03/python/examples/accounts/termsofservices/v1/accept_terms_of_service_sample.py"]]