List GBP accounts

Merchant API code sample to list GBP accounts.

Java

// 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.

package shopping.merchant.samples.accounts.gbpaccounts.v1beta;

import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.shopping.merchant.accounts.v1beta.AccountName;
import com.google.shopping.merchant.accounts.v1beta.GbpAccount;
import com.google.shopping.merchant.accounts.v1beta.GbpAccountsServiceClient;
import com.google.shopping.merchant.accounts.v1beta.GbpAccountsServiceClient.ListGbpAccountsPagedResponse;
import com.google.shopping.merchant.accounts.v1beta.GbpAccountsServiceSettings;
import com.google.shopping.merchant.accounts.v1beta.ListGbpAccountsRequest;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;

/**
 * This class demonstrates how to get the list of GBP accounts for a given Merchant Center account
 */
public class ListGbpAccountsSample {

  public static void listGbpAccounts(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.
    GbpAccountsServiceSettings gbpAccountsServiceSettings =
        GbpAccountsServiceSettings.newBuilder()
            .setCredentialsProvider(FixedCredentialsProvider.create(credential))
            .build();

    String accountId = config.getAccountId().toString();
    // Creates parent to identify the omnichannelSetting from which to list all Lfp Providers.
    String parent = AccountName.newBuilder().setAccount(accountId).build().toString();

    // Calls the API and catches and prints any network failures/errors.
    try (GbpAccountsServiceClient gbpAccountsServiceClient =
        GbpAccountsServiceClient.create(gbpAccountsServiceSettings)) {
      ListGbpAccountsRequest request =
          ListGbpAccountsRequest.newBuilder().setParent(parent).build();

      System.out.println("Sending list GBP accounts request:");
      ListGbpAccountsPagedResponse response = gbpAccountsServiceClient.listGbpAccounts(request);

      int count = 0;

      // Iterates over all the entries in the response.
      for (GbpAccount gbpAccount : response.iterateAll()) {
        System.out.println(gbpAccount);
        count++;
      }
      System.out.println(String.format("The following count of elements were returned: %d", count));
    } catch (Exception e) {
      System.out.println("An error has occured: ");
      System.out.println(e);
    }
  }

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

    listGbpAccounts(config);
  }
}

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.
"""Demonstrates how to get the list of GBP accounts for a given Merchant Center account."""

from examples.authentication import configuration
from examples.authentication import generate_user_credentials
from google.shopping.merchant_accounts_v1beta import GbpAccountsServiceClient
from google.shopping.merchant_accounts_v1beta import ListGbpAccountsRequest

# Gets the merchant account ID from the configuration file.
_ACCOUNT = configuration.Configuration().read_merchant_info()
# Creates the parent resource name string.
_PARENT = f"accounts/{_ACCOUNT}"


def list_gbp_accounts() -> None:
  """Lists the Google Business Profile accounts for a given Merchant Center account."""

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

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

  # Creates the request.
  request = ListGbpAccountsRequest(parent=_PARENT)

  # Makes the request and catches and prints any error messages.
  try:
    print("Sending list GBP accounts request:")
    # Makes the request and retrieves the list of GBP accounts.
    response = client.list_gbp_accounts(request=request)

    count = 0
    # Iterates over all the GBP accounts in the response and prints them.
    for gbp_account in response:
      print(gbp_account)
      count += 1
    print(f"The following count of elements were returned: {count}")
  except RuntimeError as e:
    print("An error has occured: ")
    print(e)


if __name__ == "__main__":
  list_gbp_accounts()