异步列出账号问题
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
用于异步列出账号问题的 Merchant API 代码示例。
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.v1;
import static com.google.api.core.ApiFutures.transform;
import com.google.api.core.ApiFuture;
import com.google.api.core.ApiFutureCallback;
import com.google.api.core.ApiFutures;
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.shopping.merchant.accounts.v1.AccountIssueServiceClient;
import com.google.shopping.merchant.accounts.v1.AccountIssueServiceSettings;
import com.google.shopping.merchant.accounts.v1.AccountName;
import com.google.shopping.merchant.accounts.v1.AccountsServiceClient;
import com.google.shopping.merchant.accounts.v1.AccountsServiceClient.ListSubAccountsPagedResponse;
import com.google.shopping.merchant.accounts.v1.AccountsServiceSettings;
import com.google.shopping.merchant.accounts.v1.ListAccountIssuesRequest;
import com.google.shopping.merchant.accounts.v1.ListAccountIssuesResponse;
import com.google.shopping.merchant.accounts.v1.ListSubAccountsRequest;
import java.io.IOException;
import java.util.AbstractMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.Executor;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;
/**
* This class demonstrates how to list the account issues of all the sub-accounts of an advanced
* account.
*/
public class ListAdvancedAccountIssuesAsyncSample {
/** Returns the list of issues for the given account. */
private static ApiFuture<ListAccountIssuesResponse> getAccountIssues(
AccountIssueServiceClient accountIssueServiceClient, String account) {
return accountIssueServiceClient
.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.
*/
private static ApiFuture<Map<String, ListAccountIssuesResponse>> getSubAccountIssues(
AccountsServiceClient accountsServiceClient,
AccountIssueServiceClient accountIssueServiceClient,
String advancedAccount)
throws IOException {
// Creates a direct executor to run the transform functions.
Executor executor = MoreExecutors.directExecutor();
// The parent has the format: accounts/{account}
ListSubAccountsRequest request =
ListSubAccountsRequest.newBuilder().setProvider(advancedAccount).build();
System.out.println("Sending list subaccounts request:");
// Lists all sub-accounts of the advanced account.
ListSubAccountsPagedResponse listSubAccountsResponse =
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()),
(ListAccountIssuesResponse response) ->
new AbstractMap.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.
return transform(
accountIssuesList,
(List<AbstractMap.SimpleEntry<String, ListAccountIssuesResponse>> list) ->
list.stream()
.collect(
Collectors.toMap(
AbstractMap.SimpleEntry::getKey,
AbstractMap.SimpleEntry::getValue,
(a, b) -> a)),
executor);
}
public static void listAccountIssues(Config config) throws Exception {
// Obtains OAuth token based on the user's configuration.
GoogleCredentials credential = new Authenticator().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.
String accountId = config.getAccountId().toString();
// Creates account name to identify account.
String parent = AccountName.newBuilder().setAccount(accountId).build().toString();
// Creates service settings using the credentials retrieved above.
AccountsServiceSettings accountsServiceSettings =
AccountsServiceSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(credential))
.build();
// 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 (AccountsServiceClient accountsServiceClient =
AccountsServiceClient.create(accountsServiceSettings);
AccountIssueServiceClient accountIssueServiceClient =
AccountIssueServiceClient.create(accountIssueServiceSettings)) {
ApiFuture<Map<String, ListAccountIssuesResponse>> subAccountIssues =
getSubAccountIssues(accountsServiceClient, accountIssueServiceClient, parent);
ApiFutures.addCallback(
subAccountIssues,
new ApiFutureCallback<Map<String, ListAccountIssuesResponse>>() {
@Override
public void onSuccess(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());
}
}
@Override
public void onFailure(Throwable throwable) {
System.out.println(throwable);
}
},
MoreExecutors.directExecutor());
} 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);
}
}
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-21。
[null,null,["最后更新时间 (UTC):2025-08-21。"],[[["\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,["# List account issues asynchronously\n\nMerchant API code sample to list account issues asynchronously. \n\n### Java\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.v1;\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.v1.AccountIssueServiceClient;\n import com.google.shopping.merchant.accounts.v1.AccountIssueServiceSettings;\n import com.google.shopping.merchant.accounts.v1.AccountName;\n import com.google.shopping.merchant.accounts.v1.AccountsServiceClient;\n import com.google.shopping.merchant.accounts.v1.AccountsServiceClient.ListSubAccountsPagedResponse;\n import com.google.shopping.merchant.accounts.v1.AccountsServiceSettings;\n import com.google.shopping.merchant.accounts.v1.ListAccountIssuesRequest;\n import com.google.shopping.merchant.accounts.v1.ListAccountIssuesResponse;\n import com.google.shopping.merchant.accounts.v1.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 /**\n * This class demonstrates how to list the account issues of all the sub-accounts of an advanced\n * account.\n */\n public class ListAdvancedAccountIssuesAsyncSample {\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 advanced account\n * as input.\n */\n private static ApiFuture\u003cMap\u003cString, ListAccountIssuesResponse\u003e\u003e getSubAccountIssues(\n AccountsServiceClient accountsServiceClient,\n AccountIssueServiceClient accountIssueServiceClient,\n String advancedAccount)\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 =\n ListSubAccountsRequest.newBuilder().setProvider(advancedAccount).build();\n System.out.println(\"Sending list subaccounts request:\");\n\n // Lists all sub-accounts of the advanced account.\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 advanced account 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/c6de994268c785ce22af0065932518a9ac5b3c03/java/src/main/java/shopping/merchant/samples/accounts/accountissues/v1/ListAdvancedAccountIssuesAsyncSample.java"]]