Retrieve for Application Terms of Service Agreement State

Merchant API Code Sample to Retrieve for Application Terms of Service Agreement State

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.v1beta;
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.shopping.merchant.accounts.v1beta.RetrieveForApplicationTermsOfServiceAgreementStateRequest;
import com.google.shopping.merchant.accounts.v1beta.TermsOfServiceAgreementState;
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 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\V1beta\Client\TermsOfServiceAgreementStateServiceClient;
use Google\Shopping\Merchant\Accounts\V1beta\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_v1beta import RetrieveForApplicationTermsOfServiceAgreementStateRequest
from google.shopping.merchant_accounts_v1beta 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()