通过 accounts.list 方法,您可以检索经过身份验证的用户可以访问的 Account 资源列表。您可以使用 filter 查询参数根据各种条件(例如以下条件)缩小结果范围:
- 账号属性
- 与其他账号的关系(例如,高级账号结构中的提供商)
- 与账号关联的服务
这有助于管理多个账号,或查找符合特定条件的特定商家账号。
您可以使用以下字段在 account 级别进行过滤:
- access:按用户对- account的访问权限类型进行过滤。此过滤条件接受以下值:- DIRECT:仅返回用户可直接访问的账号。
- INDIRECT:仅返回用户具有间接访问权限的账号。
- ALL:返回用户有权访问的所有账号(包括直接访问和间接访问)。如果未指定过滤条件,则这是默认行为。
 
- capabilities:按- account资源的- capabilities进行过滤(请注意,此字段在资源本身上不可用)。仅支持- CAN_UPLOAD_PRODUCTS功能。此字段支持否定,并使用集合语法。
- relationship(...):按账号与其他账号的关系类型进行过滤。您可以在一个请求中包含多个- relationship(...)过滤条件。
- accountName:按- account资源的- accountName进行过滤。
如需详细了解过滤条件语法,请参阅过滤条件语法指南。
示例
以下示例说明了如何构建最典型的查询。以下所有示例都使用 accounts.list 方法。如需了解详情,请参阅 accounts.list 参考文档。
查找特定提供商的子账号
accounts.listSubaccounts 方法提供了一种列出子账号的直接方式。您还可以使用以下部分中所述的过滤功能。如果您管理的是高级账号,则可以通过过滤 providerId 来列出其所有子账号。将 PROVIDER_ID 替换为您的高级账号的 ID。
例如,如果提供方的 ID 为 123,则使用 relationship(providerId=123)。
这有助于管理账号结构。
GET https://merchantapi.googleapis.com/accounts/v1/accounts?filter=relationship(providerId%20%3D%20PROVIDER_ID)
如果请求成功,则会返回 200 状态代码和包含匹配子账号列表的响应正文:
{
  "accounts": [
    {
      "name": "accounts/77777",
      "accountId": "77777",
      "accountName": "SubAccount A of Provider",
      "adultContent": false,
      "languageCode": "fr",
      "timeZone": {
        "id": "Europe/Paris"
      }
    },
    {
      "name": "accounts/88888",
      "accountId": "88888",
      "accountName": "SubAccount B of Provider",
      "adultContent": false,
      "languageCode": "de",
      "timeZone": {
        "id": "Europe/Berlin"
      }
    }
  ],
  "nextPageToken": "XYZ123abcDEF..."
}
查找无法上传商品的账号
您可以组合使用多个过滤条件,以创建更具体的搜索。
过滤条件 accountName=*store* AND -capabilities:CAN_UPLOAD_PRODUCTS 会查找名称中包含“store”的所有账号,这些账号未配置为直接上传商品。- 位于 capabilities 之前的 - 充当否定运算符。这对于仅检索高级账号非常有用。
GET https://merchantapi.googleapis.com/accounts/v1/accounts?filter=accountName%20%3D%20%22*store*%22%20AND%20-capabilities%3ACAN_UPLOAD_PRODUCTS
成功的请求会返回 200 状态代码和包含匹配账号列表的响应正文:
{
  "accounts": [
    {
      "name": "accounts/54321",
      "accountId": "54321",
      "accountName": "Partner Store - US",
      "adultContent": false,
      "languageCode": "en",
      "timeZone": {
        "id": "America/New_York"
      }
    },
    {
      "name": "accounts/98765",
      "accountId": "98765",
      "accountName": "Auxiliary Brand Store",
      "adultContent": false,
      "languageCode": "fr",
      "timeZone": {
        "id": "Europe/Paris"
      }
    }
  ],
  "nextPageToken": "CDEfghIJKlmnOPQ..."
}
按名称查找账号
您可以搜索显示名称与特定模式匹配的账号。
例如,accountName=*store* 会查找名称中包含“store”的所有账号。
这有助于快速找到特定的商家账号。
GET https://merchantapi.googleapis.com/accounts/v1/accounts?filter=accountName%20%3D%20%22*store*%22
成功的请求会返回 200 状态代码和包含匹配账号列表的响应正文:
{
  "accounts": [
    {
      "name": "accounts/12345",
      "accountId": "12345",
      "accountName": "My Awesome Store",
      "adultContent": false,
      "languageCode": "en",
      "timeZone": {
        "id": "America/Los_Angeles"
      }
    },
    {
      "name": "accounts/67890",
      "accountId": "67890",
      "accountName": "Another Store Online",
      "adultContent": false,
      "languageCode": "en",
      "timeZone": {
        "id": "Europe/London"
      }
    }
  ],
  "nextPageToken": "ABSdefGHIjklMNO..."
}
查找与特定服务提供商相关联的账号
您可以查找与提供商存在特定服务关系的账号。
例如,如需查找在账号汇总中汇总到提供商 PROVIDER_ID 下的所有账号,请使用过滤条件 relationship(providerId=PROVIDER_ID) AND
service(type="ACCOUNT_AGGREGATION")。
GET https://merchantapi.googleapis.com/accounts/v1/accounts?filter=relationship(providerId%20%3D%20PROVIDER_ID%20AND%20service(type%20%3D%20%22ACCOUNT_AGGREGATION%22))
成功的请求会返回 200 状态代码和包含匹配账号列表的响应正文:
{
  "accounts": [
    {
      "name": "accounts/54321",
      "accountId": "54321",
      "accountName": "Aggregated Account X",
      "adultContent": false,
      "languageCode": "en",
      "timeZone": {
        "id": "America/New_York"
      }
    }
  ]
}
根据服务关系审批状态查找账号
您可以根据账号与提供商的服务关系状态过滤账号。例如,查找所有尚未接受来自特定提供商 PROVIDER_ID 的账号关联请求 (handshakeState = "PENDING") 的账号。
例如,如需查找提供方 ID 为 123、服务类型为 ACCOUNT_MANAGEMENT 且状态为 PENDING 的账号,请使用 relationship(service(handshakeState = "PENDING" AND type =
"ACCOUNT_MANAGEMENT") AND providerId = 123)。
GET https://merchantapi.googleapis.com/accounts/v1/accounts?filter=relationship(service(handshakeState%20%3D%20%22PENDING%22%20AND%20type%20%3D%20%22ACCOUNT_MANAGEMENT%22)%20AND%20providerId%20%3D%20PROVIDER_ID)
成功的请求会返回 200 状态代码和包含匹配账号列表的响应正文:
{
  "accounts": [
    {
      "name": "accounts/98765",
      "accountId": "98765",
      "accountName": "Managed Account Y",
      "adultContent": false,
      "languageCode": "es",
      "timeZone": {
        "id": "Europe/Madrid"
      }
    }
  ]
}
使用客户端库过滤账号
以下示例演示了如何使用客户端库根据组合条件(例如账号名称和与提供商的关系)过滤账号。这些示例使用 accounts.list 方法。如需了解详情,请参阅accounts.list参考文档。
Java
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.shopping.merchant.accounts.v1.Account;
import com.google.shopping.merchant.accounts.v1.AccountsServiceClient;
import com.google.shopping.merchant.accounts.v1.AccountsServiceClient.ListAccountsPagedResponse;
import com.google.shopping.merchant.accounts.v1.AccountsServiceSettings;
import com.google.shopping.merchant.accounts.v1.ListAccountsRequest;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;
/** This class demonstrates how to filter the accounts the user making the request has access to. */
public class FilterAccountsSample {
  public static void filterAccounts(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.
    AccountsServiceSettings accountsServiceSettings =
        AccountsServiceSettings.newBuilder()
            .setCredentialsProvider(FixedCredentialsProvider.create(credential))
            .build();
    // Calls the API and catches and prints any network failures/errors.
    try (AccountsServiceClient accountsServiceClient =
        AccountsServiceClient.create(accountsServiceSettings)) {
      // Filter for accounts with display names containing "store" and a provider with the ID "123":
      String filter = "accountName = \"*store*\" AND relationship(providerId = 123)";
      // Filter for all subaccounts of account "123":
      // String filter2 = "relationship(providerId = 123 AND service(type =
      // \"ACCOUNT_AGGREGATION\"))";
      // String filter3 = "relationship(service(handshakeState = \"APPROVED\" AND type =
      // \"ACCOUNT_MANAGEMENT\") AND providerId = 123)";
      ListAccountsRequest request = ListAccountsRequest.newBuilder().setFilter(filter).build();
      System.out.println("Sending list accounts request with filter:");
      ListAccountsPagedResponse response = accountsServiceClient.listAccounts(request);
      int count = 0;
      // Iterates over all rows in all pages and prints the sub-account
      // in each row.
      // `response.iterateAll()` automatically uses the `nextPageToken` and recalls the
      // request to fetch all pages of data.
      for (Account account : response.iterateAll()) {
        System.out.println(account);
        count++;
      }
      System.out.print("The following count of elements were returned: ");
      System.out.println(count);
    } catch (Exception e) {
      System.out.println(e);
    }
  }
  public static void main(String[] args) throws Exception {
    Config config = Config.load();
    filterAccounts(config);
  }
}
PHP
use Google\ApiCore\ApiException;
use Google\Shopping\Merchant\Accounts\V1\Client\AccountsServiceClient;
use Google\Shopping\Merchant\Accounts\V1\ListAccountsRequest;
/**
 * This class demonstrates how to filter the accounts the user making the request has access to.
 */
class FilterAccounts
{
    public static function filterAccounts(array $config): 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.
        $accountsServiceClient = new AccountsServiceClient($options);
        // Calls the API and catches and prints any network failures/errors.
        try {
            // Filter for accounts with display names containing "store" and a provider with the ID "123":
            $filter = "accountName = \"*store*\" AND relationship(providerId = 123)";
            // Filter for all subaccounts of account "123":
            // $filter = "relationship(providerId = 123 AND service(type = \"ACCOUNT_AGGREGATION\"))";
            // $filter = "relationship(service(handshakeState = \"APPROVED\" AND type =
            // \"ACCOUNT_MANAGEMENT\") AND providerId = 123)";
            $request = new ListAccountsRequest(['filter' => $filter]);
            print "Sending list accounts request with filter:\n";
            $response = $accountsServiceClient->listAccounts($request);
            $count = 0;
            // Iterates over all rows in all pages and prints the sub-account
            // in each row.
            // `response.iterateAll()` automatically uses the `nextPageToken` and recalls the
            // request to fetch all pages of data.
            foreach ($response->iterateAllElements() as $account) {
                print_r($account); 
                $count++;
            }
            print "The following count of elements were returned: ";
            print $count . PHP_EOL;
        } catch (ApiException $e) {
            print $e->getMessage();
        }
    }
    public function callSample(): void
    {
        $config = Config::generateConfig();
        self::filterAccounts($config);
    }
}
$sample = new FilterAccounts();
$sample->callSample();
Python
from examples.authentication import generate_user_credentials
from google.shopping.merchant_accounts_v1 import AccountsServiceClient
from google.shopping.merchant_accounts_v1 import ListAccountsRequest
def filter_accounts():
  """Filters the accounts the user making the request has access to."""
  # Get OAuth credentials.
  credentials = generate_user_credentials.main()
  # Create a client.
  client = AccountsServiceClient(credentials=credentials)
  # Create the filter string.
  filter_string = 'accountName = "*store*" AND relationship(providerId = 123)'
  # Create the request.
  request = ListAccountsRequest(filter=filter_string)
  # Make the request and print the response.
  try:
    print("Sending list accounts request with filter:")
    response = client.list_accounts(request=request)
    count = 0
    for account in response:
      print(account)
      count += 1
    print(f"The following count of elements were returned: {count}")
  except RuntimeError as e:
    print(e)
if __name__ == "__main__":
  filter_accounts()
AppsScript
/**
 * Filters and lists accounts for which the logged-in user has access to
 */
function filterAccounts() {
  // IMPORTANT:
  // Enable the Merchant API Accounts sub-API Advanced Service and call it
  // "MerchantApiAccounts"
  // Create the filter string.
  // Documentation can be found at
  // https://developers.google.com/merchant/api/guides/accounts/filter-syntax
  const filter = 'accountName = "*store*" AND relationship(providerId = 123)';
  try {
    console.log('Sending filter Accounts request');
    let pageToken;
    let pageSize = 500;
    // Call the Accounts.list API method with a filter. Use the pageToken to iterate through
    // all pages of results.
    do {
      response =
          MerchantApiAccounts.Accounts.list({pageSize, pageToken, filter});
      for (const account of response.accounts) {
        console.log(account);
      }
      pageToken = response.nextPageToken;
    } while (pageToken);  // Exits when there is no next page token.
  } catch (e) {
    console.log('ERROR!');
    console.log(e);
  }
}
cURL
curl --location 'https://merchantapi.googleapis.com/accounts/v1/accounts?filter=accountName%20%3D%20%22*store*%22%20AND%20relationship(providerId%20%3D%20PROVIDER_ID)' \
--header 'Authorization: Bearer <API_TOKEN>'