Merchant API code sample to get an account 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.accountservices.v1;
    import com.google.api.gax.core.FixedCredentialsProvider;
    import com.google.auth.oauth2.GoogleCredentials;
    import com.google.shopping.merchant.accounts.v1.AccountService;
    import com.google.shopping.merchant.accounts.v1.AccountServiceName;
    import com.google.shopping.merchant.accounts.v1.AccountServicesServiceClient;
    import com.google.shopping.merchant.accounts.v1.AccountServicesServiceSettings;
    import com.google.shopping.merchant.accounts.v1.GetAccountServiceRequest;
    import shopping.merchant.samples.utils.Authenticator;
    import shopping.merchant.samples.utils.Config;

    /** This class demonstrates how to get an account service. */
    public class GetAccountServiceSample {

      public static void getAccountService(Config config, String service) throws Exception {

        // Obtains OAuth token based on the user's configuration.
        GoogleCredentials credential = new Authenticator().authenticate();

        // Creates service settings using the credentials retrieved above.
        AccountServicesServiceSettings accountServicesServiceSettings =
            AccountServicesServiceSettings.newBuilder()
                .setCredentialsProvider(FixedCredentialsProvider.create(credential))
                .build();

        // Gets the account ID from the config file.
        String accountId = config.getAccountId().toString();

        // Creates account service name to identify the account service.
        String name =
            AccountServiceName.newBuilder()
                .setAccount(accountId)
                .setService(service)
                .build()
                .toString();

        // Calls the API and catches and prints any network failures/errors.
        try (AccountServicesServiceClient accountServicesServiceClient =
            AccountServicesServiceClient.create(accountServicesServiceSettings)) {

          // The name has the format: accounts/{account}/services/{provider}
          GetAccountServiceRequest request =
              GetAccountServiceRequest.newBuilder().setName(name).build();

          System.out.println("Sending Get Account Service request:");
          AccountService response = accountServicesServiceClient.getAccountService(request);

          System.out.println("Retrieved Account Service below");
          System.out.println(response);
        } catch (Exception e) {
          System.out.println(e);
        }
      }

      public static void main(String[] args) throws Exception {
        Config config = Config.load();

        // Update this with the name of the service you want to get (e.g. from a previous list call).
        String service = "111~222~333";
        getAccountService(config, service);
      }
    }

### 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\AccountServicesServiceClient;
    use Google\Shopping\Merchant\Accounts\V1\GetAccountServiceRequest;

    /**
     * This class demonstrates how to get an account service.
     */
    class GetAccountServiceSample
    {
        /**
         * A helper function to create the name of the service.
         *
         * @param string $accountId The account that owns the product.
         * @param string $service The service to be retrieved.
         *
         * @return string The name has the format:
         * `accounts/{account}/services/{service}`
         */
        private static function getName(string $accountId, string $service): string
        {
            return sprintf('accounts/%s/services/%s', $accountId, $service);
        }

        /**
         * Retrieves an account service.
         *
         * @param array $config The configuration data used for authentication and
         *     getting the account ID.
         * @param string $service The service to be retrieved.
         */
        public static function getAccountService(
            array $config,
            string $service
        ): void {
            // Gets the OAuth credentials to make the request.
            $credentials = Authentication::useServiceAccountOrTokenFile();

            // Creates options containing credentials for the client to use.
            $options = ['credentials' => $credentials];

            // Creates a client.
            $accountServicesServiceClient = new AccountServicesServiceClient($options);

            // Gets the account ID from the config file.
            $accountId = $config['accountId'];

            // Creates account service name to identify the account service.
            $name = self::getName($accountId, $service);

            // Calls the API and catches and prints any network failures/errors.
            try {
                // The name has the format: accounts/{account}/services/{provider}
                $request = (new GetAccountServiceRequest())
                    ->setName($name);

                print "Sending Get Account Service request:\n";
                $response = $accountServicesServiceClient->getAccountService($request);

                printf("Retrieved Account Service below:%s", PHP_EOL);
                print $response->serializeToJsonString(true) . PHP_EOL;
            } catch (ApiException $e) {
                printf("An error has occurred: %s%s", $e->getMessage(), PHP_EOL);
            }
        }

        /**
         * Helper to execute the sample.
         */
        public function callSample(): void
        {
            $config = Config::generateConfig();

            // Update this with the name of the service you want to get
            // (e.g. from a previous list call).
            $service = '111~222~333';
            self::getAccountService($config, $service);
        }
    }

    // Run the script
    $sample = new GetAccountServiceSample();
    $sample->callSample();

### Python

    # -*- coding: utf-8 -*-
    # 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
    #
    #     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.
    """This class demonstrates how to get an account service."""

    from examples.authentication import configuration
    from examples.authentication import generate_user_credentials
    from google.shopping.merchant_accounts_v1 import AccountServicesServiceClient
    from google.shopping.merchant_accounts_v1 import GetAccountServiceRequest

    _ACCOUNT = configuration.Configuration().read_merchant_info()


    def get_account_service(service: str) -> None:
      """Gets an account service.

      Args:
        service: The service to get. This is the ID of the service, not the full
          resource name.
      """
      # Gets OAuth Credentials.
      credentials = generate_user_credentials.main()

      # Creates a client.
      client = AccountServicesServiceClient(credentials=credentials)

      # The name has the format: accounts/{account}/services/{provider}
      name = f"accounts/{_ACCOUNT}/services/{service}"

      # Creates the request.
      request = GetAccountServiceRequest(name=name)

      # Makes the request and catches and prints any error messages.
      try:
        print("Sending Get Account Service request:")
        response = client.get_account_service(request=request)
        print("Retrieved Account Service below")
        print(response)
      except RuntimeError as e:
        print(e)


    if __name__ == "__main__":
      # Update this with the name of the service you want to get (e.g. from a
      # previous list call).
      service_ = "111~222~333"
      get_account_service(service_)