AI-generated Key Takeaways
-
This 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.
-
The code samples show how to create an
AcceptTermsOfServiceRequestwith the account ID, Terms of Service version, and region code. -
Authentication using OAuth credentials is required, and the samples illustrate how to retrieve these credentials to use within the request.
-
The provided examples are structured to send a request to the
TermsOfServiceServiceClientto accept the specified terms, handling any potential API or network errors. -
The
acceptTermsOfServicefunction 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.
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.
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();

