Stay organized with collections
Save and categorize content based on your preferences.
Merchant API code sample to accept terms of service.
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.packageshopping.merchant.samples.accounts.termsofservices.v1;importcom.google.api.gax.core.FixedCredentialsProvider;importcom.google.auth.oauth2.GoogleCredentials;importcom.google.shopping.merchant.accounts.v1.AcceptTermsOfServiceRequest;importcom.google.shopping.merchant.accounts.v1.TermsOfServiceServiceClient;importcom.google.shopping.merchant.accounts.v1.TermsOfServiceServiceSettings;importshopping.merchant.samples.utils.Authenticator;importshopping.merchant.samples.utils.Config;/** This class demonstrates how to accept the TermsOfService agreement in a given account. */publicclassAcceptTermsOfServiceSample{publicstaticvoidacceptTermsOfService(StringaccountId,StringtosVersion,StringregionCode)throwsException{// Obtains OAuth token based on the user's configuration.GoogleCredentialscredential=newAuthenticator().authenticate();// Creates service settings using the credentials retrieved above.TermsOfServiceServiceSettingstosServiceSettings=TermsOfServiceServiceSettings.newBuilder().setCredentialsProvider(FixedCredentialsProvider.create(credential)).build();// Calls the API and catches and prints any network failures/errors.try(TermsOfServiceServiceClienttosServiceClient=TermsOfServiceServiceClient.create(tosServiceSettings)){// The parent has the format: accounts/{account}AcceptTermsOfServiceRequestrequest=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(Exceptione){System.out.println(e);}}publicstaticvoidmain(String[]args)throwsException{Configconfig=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/** * 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."""fromexamples.authenticationimportconfigurationfromexamples.authenticationimportgenerate_user_credentialsfromgoogle.shopping.merchant_accounts_v1importAcceptTermsOfServiceRequestfromgoogle.shopping.merchant_accounts_v1importTermsOfServiceServiceClient# 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 codedefaccept_terms_of_service():"""Accepts the Terms of Service agreement for a given account."""credentials=generate_user_credentials.main()client=TermsOfServiceServiceClient(credentials=credentials)# Construct the requestrequest=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.")exceptRuntimeErrorase:print(e)if__name__=="__main__":accept_terms_of_service()
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-13 UTC."],[[["\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,["Merchant API code sample to accept terms of service \n\nJava \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.v1beta;\n import com.google.api.gax.core.FixedCredentialsProvider;\n import com.google.auth.oauth2.GoogleCredentials;\n import com.google.shopping.merchant.accounts.v1beta.AcceptTermsOfServiceRequest;\n import com.google.shopping.merchant.accounts.v1beta.TermsOfServiceServiceClient;\n import com.google.shopping.merchant.accounts.v1beta.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/9105060072cf14e232bf8e3d3d3964a659b10984/java/src/main/java/shopping/merchant/samples/accounts/termsofservices/v1beta/AcceptTermsOfServiceSample.java\n\nPHP \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\\V1beta\\AcceptTermsOfServiceRequest;\n use Google\\Shopping\\Merchant\\Accounts\\V1beta\\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/9105060072cf14e232bf8e3d3d3964a659b10984/php/examples/accounts/termsofservices/v1beta/AcceptTermsOfServiceSample.php\n\nPython \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_v1beta import AcceptTermsOfServiceRequest\n from google.shopping.merchant_accounts_v1beta 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/9105060072cf14e232bf8e3d3d3964a659b10984/python/examples/accounts/termsofservices/v1beta/accept_terms_of_service_sample.py"]]