Liệt kê tài khoản có thể truy cập

Bạn có thể liệt kê các khách hàng mà bạn có thể truy cập bằng phương thức ListAccessibleCustomers trong CustomerService. Tuy nhiên, bạn cần hiểu rõ khách hàng nào được trả về trong loại yêu cầu này.

Việc liệt kê khách hàng có thể truy cập là một trong số ít yêu cầu trong API Google Ads không yêu cầu bạn chỉ định mã khách hàng trong yêu cầu và sẽ bỏ qua mọi login-customer-id được cung cấp.

Danh sách khách hàng thu được dựa trên thông tin đăng nhập OAuth của bạn. Yêu cầu này sẽ trả về danh sách tất cả các tài khoản mà bạn có thể thao tác trực tiếp dựa trên thông tin đăng nhập hiện tại của bạn. Dữ liệu này không nhất thiết sẽ bao gồm tất cả các tài khoản trong hệ thống phân cấp tài khoản; thay vào đó, dữ liệu này sẽ chỉ bao gồm các tài khoản mà người dùng đã xác thực của bạn đã được thêm cùng với các quyền quản trị hoặc các quyền khác trong tài khoản.

Hãy tưởng tượng bạn là người dùng A, là quản trị viên của M1C3 ở hai hệ phân cấp như hình trên. Nếu thực hiện lệnh gọi đến API Google Ads, ví dụ: đến GoogleAdsService, thì bạn có thể truy cập vào thông tin cho các tài khoản M1, C1, C2C3. Tuy nhiên, lệnh gọi đến CustomerService.ListAccessibleCustomers sẽ chỉ trả về M1C3 vì đó là những tài khoản duy nhất mà người dùng A có quyền truy cập trực tiếp.

Dưới đây là mã ví dụ minh hoạ việc sử dụng phương thức CustomerService.ListAccessibleCustomers:

Java

private void runExample(GoogleAdsClient client) {
  // Optional: Change credentials to use a different refresh token, to retrieve customers
  //           available for a specific user.
  //
  // UserCredentials credentials =
  //     UserCredentials.newBuilder()
  //         .setClientId("INSERT_OAUTH_CLIENT_ID")
  //         .setClientSecret("INSERT_OAUTH_CLIENT_SECRET")
  //         .setRefreshToken("INSERT_REFRESH_TOKEN")
  //         .build();
  //
  // client = client.toBuilder().setCredentials(credentials).build();

  try (CustomerServiceClient customerService =
      client.getLatestVersion().createCustomerServiceClient()) {
    ListAccessibleCustomersResponse response =
        customerService.listAccessibleCustomers(
            ListAccessibleCustomersRequest.newBuilder().build());

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

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

C#

public void Run(GoogleAdsClient client)
{
    // Get the CustomerService.
    CustomerServiceClient customerService = client.GetService(Services.V16.CustomerService);

    try
    {
        // Retrieve the list of customer resources.
        string[] customerResourceNames = customerService.ListAccessibleCustomers();

        // Display the result.
        foreach (string customerResourceName in customerResourceNames)
        {
            Console.WriteLine(
                $"Found customer with resource name = '{customerResourceName}'.");
        }
    }
    catch (GoogleAdsException e)
    {
        Console.WriteLine("Failure:");
        Console.WriteLine($"Message: {e.Message}");
        Console.WriteLine($"Failure: {e.Failure}");
        Console.WriteLine($"Request ID: {e.RequestId}");
        throw;
    }
}
      

1.199

public static function runExample(GoogleAdsClient $googleAdsClient)
{
    $customerServiceClient = $googleAdsClient->getCustomerServiceClient();

    // Issues a request for listing all accessible customers.
    $accessibleCustomers =
        $customerServiceClient->listAccessibleCustomers(new ListAccessibleCustomersRequest());
    print 'Total results: ' . count($accessibleCustomers->getResourceNames()) . PHP_EOL;

    // Iterates over all accessible customers' resource names and prints them.
    foreach ($accessibleCustomers->getResourceNames() as $resourceName) {
        /** @var string $resourceName */
        printf("Customer resource name: '%s'%s", $resourceName, PHP_EOL);
    }
}
      

Python

def main(client):
    customer_service = client.get_service("CustomerService")

    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'Customer resource name: "{resource_name}"')
      

Ruby

def list_accessible_customers()
  # GoogleAdsClient will read a config file from
  # ENV['HOME']/google_ads_config.rb when called without parameters
  client = Google::Ads::GoogleAds::GoogleAdsClient.new

  accessible_customers = client.service.customer.list_accessible_customers().resource_names

  accessible_customers.each do |resource_name|
    puts "Customer resource name: #{resource_name}"
  end
end
      

Perl

sub list_accessible_customers {
  my ($api_client) = @_;

  my $list_accessible_customers_response =
    $api_client->CustomerService()->list_accessible_customers();

  printf "Total results: %d.\n",
    scalar @{$list_accessible_customers_response->{resourceNames}};

  foreach
    my $resource_name (@{$list_accessible_customers_response->{resourceNames}})
  {
    printf "Customer resource name: '%s'.\n", $resource_name;
  }

  return 1;
}
      

Liệt kê tài khoản đã huỷ

API Google Ads không cung cấp cách trực tiếp để liệt kê các tài khoản đã hủy trong Tài khoản người quản lý. Tuy nhiên, bạn có thể sử dụng giải pháp sau để truy xuất danh sách này.

  1. Truy xuất danh sách đường liên kết ACTIVE bằng tài nguyên customer_client_link và tạo danh sách khách hàng bằng trường customer_client_link.client_customer.

    SELECT customer_client_link.client_customer, customer_client_link.status FROM
        customer_client_link WHERE customer_client_link.status = ACTIVE
    
  2. Truy xuất danh sách tài khoản ENABLED bằng tài nguyên customer_client.

    SELECT customer_client.id, customer_client.descriptive_name FROM customer_client
    
  3. Sự khác biệt giữa hai danh sách này cung cấp cho bạn danh sách các tài khoản đã huỷ.