列出可访问的帐号

您可以使用 CustomerService 中的 ListAccessibleCustomers 方法列出自己可访问的客户。不过,有必要了解一下此类请求会返回哪些客户。

在 Search Ads 360 Reporting API 中,有少数请求不仅不要求您在请求中指定客户 ID,而且还会忽略任何已提供的 login-customer-id,列出可访问的客户就是此类请求之一。

生成的客户列表取决于您的 OAuth 凭据。该请求会返回一个列表,其中会列出您可以根据当前凭据直接采取操作的所有账号。它不一定包括账号层次结构中的所有账号,只会包括已通过账号中的管理员权限或其他权限添加了身份验证成功用户的账号。

假设您是用户 A,同时是上图两个层次结构中 M1C3 的管理员。如果您要调用 Search Ads 360 Reporting API(例如调用 SearchAds360Service),则可以访问账号 M1C1C2C3 的信息。不过,对 CustomerService.ListAccessibleCustomers 的调用将仅返回 M1C3,因为只有这两个账号是用户 A 可以直接访问的账号。

以下示例代码说明了如何使用 CustomerService.ListAccessibleCustomers 方法:

Java

// Copyright 2022 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 sample;

import com.google.ads.searchads360.v0.lib.SearchAds360Client;
import com.google.ads.searchads360.v0.services.CustomerServiceClient;
import com.google.ads.searchads360.v0.services.ListAccessibleCustomersRequest;
import com.google.ads.searchads360.v0.services.ListAccessibleCustomersResponse;

/** List all customers that can be accessed by the authenticated Google account. */
public class ListAccessibleCustomers {

  public static void main(String[] args) {
    try {
      // Creates a SearchAds360Client with local properties file
      final SearchAds360Client searchAds360Client =
          SearchAds360Client.newBuilder().fromPropertiesFile().build();
      // Creates the Customer Service Client.
      CustomerServiceClient client = searchAds360Client.createCustomerServiceClient();
      new ListAccessibleCustomers().runExample(client);
    } catch (Exception exception) {
      System.err.printf("Failed with exception: %s%n", exception);
      exception.printStackTrace();
      System.exit(1);
    }
  }

  private void runExample(CustomerServiceClient customerServiceClient) {
    ListAccessibleCustomersResponse response =
        customerServiceClient.listAccessibleCustomers(
            ListAccessibleCustomersRequest.getDefaultInstance());

    System.out.printf("Total results: %d%n", response.getResourceNamesCount());

    for (String customerResourceName : response.getResourceNamesList()) {
      System.out.printf("Customer resource name: %s%n", customerResourceName);
    }
  }
}

下载 ListAccessibleCustomers.java

Python

#!/usr/bin/env python
# Copyright 2022 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 accessible customers."""

import traceback
from util_searchads360 import SearchAds360Client


def main(client) -> None:
  customer_service = client.get_customer_service()

  # Issues a list accessible customer request.
  accessible_customers = customer_service.list_accessible_customers()
  result_total = len(accessible_customers.resource_names)
  print(f"Total results: {result_total}")

  resource_names = accessible_customers.resource_names
  for resource_name in resource_names:
    print(f'Accessible customer resource name: "{resource_name}"')


if __name__ == "__main__":
  # SearchAds360Client will read the search-ads-360.yaml configuration file in
  # the home directory if none is specified.
  search_ads_360_client = SearchAds360Client.load_from_file()

  try:
    main(search_ads_360_client)
  except Exception:  # pylint: disable=broad-except
    traceback.print_exc()

下载 list_accessible_customers.py