List account issues

Merchant API code sample to list account issues

AppsScript

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

/**
 * Lists all issues for a given Merchant Center account.
 */
function listAccountIssues() {
  // IMPORTANT:
  // Enable the Merchant API Accounts Bundle Advanced Service and call it
  // "MerchantApiAccounts"

  // Replace this with your Merchant Center ID.
  const accountId = "<MERCHANT_CENTER_ID>";

  // Construct the parent name
  const parent = 'accounts/' + accountId;

  try {
    console.log('Sending list Account Issues request');
    // Set pageSize to the maximum value (default: 50)
    let pageSize = 100;
    let pageToken;
    let count = 0;
    // Call the Account.Issues.list API method. Use the pageToken to iterate
    // through all pages of results.
    do {
      response = MerchantApiAccounts.Accounts.Issues.list(parent, {pageSize, pageToken});
      for (const issue of response.accountIssues) {
        console.log(issue);
        count++;
      }
      pageToken = response.nextPageToken;
    } while (pageToken);  // Exits when there is no next page token.
    console.log('The following count of Account Issues were returned: ', count);
  } catch (e) {
    console.log('ERROR!');
    console.log(e);
  }
}

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.accountissues.v1beta;

import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.shopping.merchant.accounts.v1beta.AccountIssue;
import com.google.shopping.merchant.accounts.v1beta.AccountIssueServiceClient;
import com.google.shopping.merchant.accounts.v1beta.AccountIssueServiceClient.ListAccountIssuesPagedResponse;
import com.google.shopping.merchant.accounts.v1beta.AccountIssueServiceSettings;
import com.google.shopping.merchant.accounts.v1beta.AccountName;
import com.google.shopping.merchant.accounts.v1beta.ListAccountIssuesRequest;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;

/**
 * This class demonstrates how to list all the account issues of an account.
 *
 * If you want to query the account issues of all the sub-accounts of an MCA, see
 * ListMcaAccountIssuesSampleAsync.
 */
public class ListAccountIssuesSample {

  
  public static void listAccountIssues(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.
    AccountIssueServiceSettings accountIssueServiceSettings =
        AccountIssueServiceSettings.newBuilder()
            .setCredentialsProvider(FixedCredentialsProvider.create(credential))
            .build();

    // Calls the API and catches and prints any network failures/errors.
    try (AccountIssueServiceClient accountIssueServiceClient =
        AccountIssueServiceClient.create(accountIssueServiceSettings)) {

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

      // Creates account name to identify account.
      String name = AccountName.newBuilder().setAccount(accountId).build().toString();
      ListAccountIssuesRequest request =
          ListAccountIssuesRequest.newBuilder().setParent(name).build();

      System.out.println("Sending list account issues request:");
      ListAccountIssuesPagedResponse response =
          accountIssueServiceClient.listAccountIssues(request);

      int count = 0;

      // Iterates over all rows in all pages and prints the issue in each row.
      // Automatically uses the `nextPageToken` if returned to fetch all pages of data.
      for (AccountIssue accountIssue : response.iterateAll()) {
        System.out.println(accountIssue);
        count++;
      }
      System.out.print("The following count of account issues were returned: ");
      System.out.println(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();
    listAccountIssues(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\AccountIssueServiceClient;
use Google\Shopping\Merchant\Accounts\V1beta\ListAccountIssuesRequest;

/**
 * Lists all the account issues of an account.
 */
class ListAccountIssues
{
    /**
     * A helper function to create the parent string.
     *
     * @param array $accountId
     *      The account.
     *
     * @return string The parent has the format: `accounts/{account_id}`
     */
    private static function getParent($accountId)
    {
        return sprintf("accounts/%s", $accountId);
    }

    /**
     * Lists all the account issues for a given Merchant Center account.
     *
     * @param array $config
     *      The configuration data used for authentication and getting the acccount ID.
     * @return void
     */
    public static function listAccountIssuesSample($config): void
    {
        // Gets the OAuth credentials.
        $credentials = Authentication::useServiceAccountOrTokenFile();

        // Creates options config.
        $options = ['credentials' => $credentials];

        // Creates a client.
        $accountIssueServiceClient = new AccountIssueServiceClient($options);

        // Creates parent.
        $parent = self::getParent($config['accountId']);

        // Creates the request.
        $request = new ListAccountIssuesRequest(['parent' => $parent]);

        // Calls the API and catches and prints any network failures/errors.
        try {
            print "Sending list account issues request:\n";
            $response = $accountIssueServiceClient->listAccountIssues($request);

            $count = 0;
            // Iterates over all elements and prints the issue in each row.
            foreach ($response->iterateAllElements() as $accountIssue) {
                print_r($accountIssue);
                $count++;
            }
            print "The following count of account issues were returned: ";
            print $count . "\n";
        } catch (ApiException $e) {
            print "An error has occured: \n";
            print $e->getMessage() . "\n";
        }
    }

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

        // Lists the account issues.
        self::listAccountIssuesSample($config);
    }
}

// Run the script
$sample = new ListAccountIssues();
$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.
"""A module to list all the account issues of an account."""


from examples.authentication import configuration
from examples.authentication import generate_user_credentials
from google.shopping.merchant_accounts_v1beta import AccountIssueServiceClient
from google.shopping.merchant_accounts_v1beta import ListAccountIssuesRequest

_ACCOUNT = configuration.Configuration().read_merchant_info()
_PARENT = f"accounts/{_ACCOUNT}"


def list_account_issues():
  """Lists all the account issues of an account."""

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

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

  request = ListAccountIssuesRequest(parent=_PARENT)

  print("Sending list account issues request:")

  # Makes the request and catches and prints any error messages.
  try:
    response = client.list_account_issues(request=request)
    count = 0

    for account_issue in response:
      print(account_issue)
      count += 1
    print("The following count of account issues were returned: ")
    print(count)

  except RuntimeError as e:
    print("An error has occured: ")
    print(e)


if __name__ == "__main__":
  list_account_issues()