Stay organized with collections
Save and categorize content based on your preferences.
Merchant API code sample to list account issues asynchronously.
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.packageshopping.merchant.samples.accounts.accountissues.v1;import staticcom.google.api.core.ApiFutures.transform;importcom.google.api.core.ApiFuture;importcom.google.api.core.ApiFutureCallback;importcom.google.api.core.ApiFutures;importcom.google.api.gax.core.FixedCredentialsProvider;importcom.google.auth.oauth2.GoogleCredentials;importcom.google.common.util.concurrent.MoreExecutors;importcom.google.shopping.merchant.accounts.v1.AccountIssueServiceClient;importcom.google.shopping.merchant.accounts.v1.AccountIssueServiceSettings;importcom.google.shopping.merchant.accounts.v1.AccountName;importcom.google.shopping.merchant.accounts.v1.AccountsServiceClient;importcom.google.shopping.merchant.accounts.v1.AccountsServiceClient.ListSubAccountsPagedResponse;importcom.google.shopping.merchant.accounts.v1.AccountsServiceSettings;importcom.google.shopping.merchant.accounts.v1.ListAccountIssuesRequest;importcom.google.shopping.merchant.accounts.v1.ListAccountIssuesResponse;importcom.google.shopping.merchant.accounts.v1.ListSubAccountsRequest;importjava.io.IOException;importjava.util.AbstractMap;importjava.util.List;importjava.util.Map;importjava.util.Map.Entry;importjava.util.concurrent.Executor;importjava.util.stream.Collectors;importjava.util.stream.StreamSupport;importshopping.merchant.samples.utils.Authenticator;importshopping.merchant.samples.utils.Config;/** * This class demonstrates how to list the account issues of all the sub-accounts of an advanced * account. */publicclassListAdvancedAccountIssuesAsyncSample{/** Returns the list of issues for the given account. */privatestaticApiFuture<ListAccountIssuesResponse>getAccountIssues(AccountIssueServiceClientaccountIssueServiceClient,Stringaccount){returnaccountIssueServiceClient.listAccountIssuesCallable().futureCall(ListAccountIssuesRequest.newBuilder().setParent(account).build());}/** * Returns a map of account issues where key is the sub-account resource name and the value is the * list of issues for each sub-account. Takes the API clients and the name of the advanced account * as input. */privatestaticApiFuture<Map<String,ListAccountIssuesResponse>>getSubAccountIssues(AccountsServiceClientaccountsServiceClient,AccountIssueServiceClientaccountIssueServiceClient,StringadvancedAccount)throwsIOException{// Creates a direct executor to run the transform functions.Executorexecutor=MoreExecutors.directExecutor();// The parent has the format: accounts/{account}ListSubAccountsRequestrequest=ListSubAccountsRequest.newBuilder().setProvider(advancedAccount).build();System.out.println("Sending list subaccounts request:");// Lists all sub-accounts of the advanced account.ListSubAccountsPagedResponselistSubAccountsResponse=accountsServiceClient.listSubAccounts(request);// Iterates over all subAccounts and lists account issues for each.// Automatically uses the `nextPageToken` if returned to fetch all pages of data.List<ApiFuture<AbstractMap.SimpleEntry<String,ListAccountIssuesResponse>>>
accountIssuesFutures=StreamSupport.stream(listSubAccountsResponse.iterateAll().spliterator(),false).map(account->
transform(getAccountIssues(accountIssueServiceClient,account.getName()),(ListAccountIssuesResponseresponse)->
newAbstractMap.SimpleEntry<>(account.getName(),response),executor)).collect(Collectors.toList());// Collects all the responses into a single future.ApiFuture<List<AbstractMap.SimpleEntry<String,ListAccountIssuesResponse>>>accountIssuesList=ApiFutures.allAsList(accountIssuesFutures);// Transforms the list of responses into a map.returntransform(accountIssuesList,(List<AbstractMap.SimpleEntry<String,ListAccountIssuesResponse>>list)->
list.stream().collect(Collectors.toMap(AbstractMap.SimpleEntry::getKey,AbstractMap.SimpleEntry::getValue,(a,b)->a)),executor);}publicstaticvoidlistAccountIssues(Configconfig)throwsException{// Obtains OAuth token based on the user's configuration.GoogleCredentialscredential=newAuthenticator().authenticate();// Gets the account ID from the config file.// Make sure to use the advanced account ID here, otherwise the API will return an error.StringaccountId=config.getAccountId().toString();// Creates account name to identify account.Stringparent=AccountName.newBuilder().setAccount(accountId).build().toString();// Creates service settings using the credentials retrieved above.AccountsServiceSettingsaccountsServiceSettings=AccountsServiceSettings.newBuilder().setCredentialsProvider(FixedCredentialsProvider.create(credential)).build();// Creates service settings using the credentials retrieved above.AccountIssueServiceSettingsaccountIssueServiceSettings=AccountIssueServiceSettings.newBuilder().setCredentialsProvider(FixedCredentialsProvider.create(credential)).build();// Calls the API and catches and prints any network failures/errors.try(AccountsServiceClientaccountsServiceClient=AccountsServiceClient.create(accountsServiceSettings);AccountIssueServiceClientaccountIssueServiceClient=AccountIssueServiceClient.create(accountIssueServiceSettings)){ApiFuture<Map<String,ListAccountIssuesResponse>>subAccountIssues=getSubAccountIssues(accountsServiceClient,accountIssueServiceClient,parent);ApiFutures.addCallback(subAccountIssues,newApiFutureCallback<Map<String,ListAccountIssuesResponse>>(){@OverridepublicvoidonSuccess(Map<String,ListAccountIssuesResponse>results){System.out.println("Account Issues");for(Entry<String,ListAccountIssuesResponse>entry:results.entrySet()){System.out.println("Issues for account "+entry.getKey());System.out.println(entry.getValue());}}@OverridepublicvoidonFailure(Throwablethrowable){System.out.println(throwable);}},MoreExecutors.directExecutor());}catch(Exceptione){System.out.println("An error has occured: ");System.out.println(e);}}publicstaticvoidmain(String[]args)throwsException{Configconfig=Config.load();listAccountIssues(config);}}
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-13 UTC."],[[["\u003cp\u003eThis code sample demonstrates how to asynchronously list account issues for all sub-accounts of a Merchant Center Account (MCA) using the Merchant API in Java.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003egetSubAccountIssues\u003c/code\u003e method retrieves a map of account issues, where each key is a sub-account's resource name, and the value is a list of its issues.\u003c/p\u003e\n"],["\u003cp\u003eThe code utilizes API Futures to perform operations asynchronously, including listing sub-accounts and retrieving issues for each sub-account, then collecting and transforming the results.\u003c/p\u003e\n"],["\u003cp\u003eThe sample leverages the \u003ccode\u003eAccountIssueServiceClient\u003c/code\u003e and \u003ccode\u003eAccountsServiceClient\u003c/code\u003e to interact with the Merchant API, requiring OAuth credentials for authentication.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003elistAccountIssues\u003c/code\u003e method sets up the necessary configurations, including obtaining OAuth tokens, getting the account ID, and then calling the API to list account issues for all sub-accounts.\u003c/p\u003e\n"]]],[],null,["Merchant API code sample to list account issues asynchronously \n\nJava \n\n // Copyright 2024 Google LLC\n //\n // Licensed under the Apache License, Version 2.0 (the \"License\");\n // you may not use this file except in compliance with the License.\n // You may obtain a copy of the License at\n //\n // https://www.apache.org/licenses/LICENSE-2.0\n //\n // Unless required by applicable law or agreed to in writing, software\n // distributed under the License is distributed on an \"AS IS\" BASIS,\n // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n // See the License for the specific language governing permissions and\n // limitations under the License.\n\n package shopping.merchant.samples.accounts.accountissues.v1beta;\n\n import static com.google.api.core.ApiFutures.transform;\n\n import com.google.api.core.ApiFuture;\n import com.google.api.core.ApiFutureCallback;\n import com.google.api.core.ApiFutures;\n import com.google.api.gax.core.FixedCredentialsProvider;\n import com.google.auth.oauth2.GoogleCredentials;\n import com.google.common.util.concurrent.MoreExecutors;\n import com.google.shopping.merchant.accounts.v1beta.AccountIssueServiceClient;\n import com.google.shopping.merchant.accounts.v1beta.AccountIssueServiceSettings;\n import com.google.shopping.merchant.accounts.v1beta.AccountName;\n import com.google.shopping.merchant.accounts.v1beta.AccountsServiceClient;\n import com.google.shopping.merchant.accounts.v1beta.AccountsServiceClient.ListSubAccountsPagedResponse;\n import com.google.shopping.merchant.accounts.v1beta.AccountsServiceSettings;\n import com.google.shopping.merchant.accounts.v1beta.ListAccountIssuesRequest;\n import com.google.shopping.merchant.accounts.v1beta.ListAccountIssuesResponse;\n import com.google.shopping.merchant.accounts.v1beta.ListSubAccountsRequest;\n import java.io.IOException;\n import java.util.AbstractMap;\n import java.util.List;\n import java.util.Map;\n import java.util.Map.Entry;\n import java.util.concurrent.Executor;\n import java.util.stream.Collectors;\n import java.util.stream.StreamSupport;\n import shopping.merchant.samples.utils.Authenticator;\n import shopping.merchant.samples.utils.Config;\n\n /** This class demonstrates how to list the account issues of all the sub-accounts of an MCA. */\n public class ListMcaAccountIssuesAsyncSample {\n\n /** Returns the list of issues for the given account. */\n private static ApiFuture\u003cListAccountIssuesResponse\u003e getAccountIssues(\n AccountIssueServiceClient accountIssueServiceClient, String account) {\n return accountIssueServiceClient\n .listAccountIssuesCallable()\n .futureCall(ListAccountIssuesRequest.newBuilder().setParent(account).build());\n }\n\n /**\n * Returns a map of account issues where key is the sub-account resource name and the value is the\n * list of issues for each sub-account. Takes the API clients and the name of the MCA as input.\n */\n private static ApiFuture\u003cMap\u003cString, ListAccountIssuesResponse\u003e\u003e getSubAccountIssues(\n AccountsServiceClient accountsServiceClient,\n AccountIssueServiceClient accountIssueServiceClient,\n String mca)\n throws IOException {\n\n // Creates a direct executor to run the transform functions.\n Executor executor = MoreExecutors.directExecutor();\n\n // The parent has the format: accounts/{account}\n ListSubAccountsRequest request = ListSubAccountsRequest.newBuilder().setProvider(mca).build();\n System.out.println(\"Sending list subaccounts request:\");\n\n // Lists all sub-accounts of the MCA.\n ListSubAccountsPagedResponse listSubAccountsResponse =\n accountsServiceClient.listSubAccounts(request);\n\n // Iterates over all subAccounts and lists account issues for each.\n // Automatically uses the `nextPageToken` if returned to fetch all pages of data.\n List\u003cApiFuture\u003cAbstractMap.SimpleEntry\u003cString, ListAccountIssuesResponse\u003e\u003e\u003e\n accountIssuesFutures =\n StreamSupport.stream(listSubAccountsResponse.iterateAll().spliterator(), false)\n .map(\n account -\u003e\n transform(\n getAccountIssues(accountIssueServiceClient, account.getName()),\n (ListAccountIssuesResponse response) -\u003e\n new AbstractMap.SimpleEntry\u003c\u003e(account.getName(), response),\n executor))\n .collect(Collectors.toList());\n\n // Collects all the responses into a single future.\n ApiFuture\u003cList\u003cAbstractMap.SimpleEntry\u003cString, ListAccountIssuesResponse\u003e\u003e\u003e accountIssuesList =\n ApiFutures.allAsList(accountIssuesFutures);\n\n // Transforms the list of responses into a map.\n return transform(\n accountIssuesList,\n (List\u003cAbstractMap.SimpleEntry\u003cString, ListAccountIssuesResponse\u003e\u003e list) -\u003e\n list.stream()\n .collect(\n Collectors.toMap(\n AbstractMap.SimpleEntry::getKey,\n AbstractMap.SimpleEntry::getValue,\n (a, b) -\u003e a)),\n executor);\n }\n\n public static void listAccountIssues(Config config) throws Exception {\n\n // Obtains OAuth token based on the user's configuration.\n GoogleCredentials credential = new Authenticator().authenticate();\n\n // Gets the account ID from the config file.\n // Make sure to use the MCA ID here, otherwise the API will return an error.\n String accountId = config.getAccountId().toString();\n\n // Creates account name to identify account.\n String parent = AccountName.newBuilder().setAccount(accountId).build().toString();\n\n // Creates service settings using the credentials retrieved above.\n AccountsServiceSettings accountsServiceSettings =\n AccountsServiceSettings.newBuilder()\n .setCredentialsProvider(FixedCredentialsProvider.create(credential))\n .build();\n\n // Creates service settings using the credentials retrieved above.\n AccountIssueServiceSettings accountIssueServiceSettings =\n AccountIssueServiceSettings.newBuilder()\n .setCredentialsProvider(FixedCredentialsProvider.create(credential))\n .build();\n\n // Calls the API and catches and prints any network failures/errors.\n try (AccountsServiceClient accountsServiceClient =\n AccountsServiceClient.create(accountsServiceSettings);\n AccountIssueServiceClient accountIssueServiceClient =\n AccountIssueServiceClient.create(accountIssueServiceSettings)) {\n\n ApiFuture\u003cMap\u003cString, ListAccountIssuesResponse\u003e\u003e subAccountIssues =\n getSubAccountIssues(accountsServiceClient, accountIssueServiceClient, parent);\n\n ApiFutures.addCallback(\n subAccountIssues,\n new ApiFutureCallback\u003cMap\u003cString, ListAccountIssuesResponse\u003e\u003e() {\n @Override\n public void onSuccess(Map\u003cString, ListAccountIssuesResponse\u003e results) {\n System.out.println(\"Account Issues\");\n for (Entry\u003cString, ListAccountIssuesResponse\u003e entry : results.entrySet()) {\n System.out.println(\"Issues for account \" + entry.getKey());\n System.out.println(entry.getValue());\n }\n }\n\n @Override\n public void onFailure(Throwable throwable) {\n System.out.println(throwable);\n }\n },\n MoreExecutors.directExecutor());\n } catch (Exception e) {\n System.out.println(\"An error has occured: \");\n System.out.println(e);\n }\n }\n\n public static void main(String[] args) throws Exception {\n Config config = Config.load();\n listAccountIssues(config);\n }\n } \n https://github.com/google/merchant-api-samples/blob/9105060072cf14e232bf8e3d3d3964a659b10984/java/src/main/java/shopping/merchant/samples/accounts/accountissues/v1beta/ListMcaAccountIssuesAsyncSample.java"]]