Merchant API code sample to get an account relationship.

### 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.accountrelationships.v1;

    import com.google.api.gax.core.FixedCredentialsProvider;
    import com.google.auth.oauth2.GoogleCredentials;
    import com.google.shopping.merchant.accounts.v1.AccountRelationship;
    import com.google.shopping.merchant.accounts.v1.AccountRelationshipName;
    import com.google.shopping.merchant.accounts.v1.AccountRelationshipsServiceClient;
    import com.google.shopping.merchant.accounts.v1.AccountRelationshipsServiceSettings;
    import com.google.shopping.merchant.accounts.v1.GetAccountRelationshipRequest;
    import shopping.merchant.samples.utils.Authenticator;
    import shopping.merchant.samples.utils.Config;

    /** This class demonstrates how to get an account relationship. */
    public class GetAccountRelationshipSample {

      public static void getAccountRelationship(Config config, long providerId) throws Exception {

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

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

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

        // Creates account relationship name to identify the account relationship.
        String name =
            AccountRelationshipName.newBuilder()
                .setAccount(accountId)
                .setRelationship(String.valueOf(providerId))
                .build()
                .toString();

        // Calls the API and catches and prints any network failures/errors.
        try (AccountRelationshipsServiceClient accountRelationshipsServiceClient =
            AccountRelationshipsServiceClient.create(accountRelationshipsServiceSettings)) {

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

          System.out.println("Sending Get Account Relationship request:");
          AccountRelationship response =
              accountRelationshipsServiceClient.getAccountRelationship(request);

          System.out.println("Retrieved Account Relationship 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 provider ID you want to get the relationship for.
        long providerId = 111L;
        getAccountRelationship(config, providerId);
      }
    }

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

    /**
     * This class demonstrates how to get an account relationship.
     */
    class GetAccountRelationshipSample
    {
        /**
         * Retrieves a specific account relationship.
         *
         * @param array $config The configuration file for authentication.
         * @param int $providerId The ID of the provider for the relationship.
         */
        public static function getAccountRelationshipSample(array $config, int $providerId): void
        {
            // Gets the OAuth credentials to make the request.
            $credentials = Authentication::useServiceAccountOrTokenFile();

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

            // Creates a client.
            $accountRelationshipsServiceClient = new AccountRelationshipsServiceClient($options);

            // The name of the account relationship to retrieve.
            // Format: accounts/{account}/relationships/{provider}
            $name = $accountRelationshipsServiceClient->accountRelationshipName(
                $config['accountId'],
                $providerId
            );

            // Creates the request.
            $request = new GetAccountRelationshipRequest([
                'name' => $name
            ]);

            // Calls the API and catches and prints any network failures/errors.
            try {
                printf("Sending Get Account Relationship request:%s", PHP_EOL);
                $response = $accountRelationshipsServiceClient->getAccountRelationship($request);
                printf("Retrieved Account Relationship below%s", PHP_EOL);
                print $response->serializeToJsonString(true) . PHP_EOL;
            } catch (ApiException $e) {
                print $e->getMessage() . PHP_EOL;
            }
        }

        /**
         * Helper to execute the sample.
         */
        public function callSample(): void
        {
            $config = Config::generateConfig();
            // Update this with the provider ID you want to get the relationship for.
            $providerId = 111;
            self::getAccountRelationshipSample($config, $providerId);
        }
    }

    // Runs the sample.
    $sample = new GetAccountRelationshipSample();
    $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.

    """Gets an account relationship.

    Retrieves an existing account relationship between a merchant and a provider.
    """
    from examples.authentication import configuration
    from examples.authentication import generate_user_credentials
    from google.shopping.merchant_accounts_v1 import AccountRelationshipsServiceClient
    from google.shopping.merchant_accounts_v1 import GetAccountRelationshipRequest

    # Gets the account ID from the configuration file.
    _ACCOUNT_ID = configuration.Configuration().read_merchant_info()


    def get_account_relationship(account_id: str, provider_id: int) -> None:
      """Gets an account relationship."""

      # Gets OAuth Credentials.
      credentials = generate_user_credentials.main()

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

      # Creates the name of the relationship to retrieve.
      # The name has the format: accounts/{account}/relationships/{provider}
      name = f"accounts/{account_id}/relationships/{provider_id}"

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

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


    if __name__ == "__main__":
      # The provider ID of the relationship to retrieve.
      provider_id_ = 111
      get_account_relationship(_ACCOUNT_ID, provider_id_)