列出可存取的帳戶

您可以透過 ListAccessibleCustomers敬上 CustomerService 中的方法。不過 ,瞭解哪些客戶會在這類要求中傳回。

列出可存取的顧客是 Google Ads API 中的少數要求之一 您不需要在請求中指定客戶 ID,且會忽略所有 已提供 login-customer-id

系統會根據您的 OAuth 憑證產生客戶清單。 要求傳回可處理的所有帳戶清單 直接提供您目前的憑證。這不一定包括 帳戶階層中的所有帳戶;而是只包含帳戶 已驗證使用者已新增至 讓他們使用服務帳戶

假設您是使用者 A,而且是兩個階層中的 M1C3 管理員 如上圖所示舉例來說,假設您要呼叫 Google Ads API GoogleAdsService,您可以存取 有關帳戶:M1C1C2 的資訊 和 C3。不過,如果呼叫 CustomerService.ListAccessibleCustomers 會 只會傳回 M1C3,因為使用者 A 只有這些帳戶 直接存取。

以下程式碼範例說明如何使用 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.V17.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;
    }
}
      

PHP

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}"')
      

小茹

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;
}
      

列出已取消的帳戶

Google Ads API 無法直接列出 管理員帳戶不過,您可以使用以下解決方法 這份清單

  1. 使用 customer_client_link 資源擷取 ACTIVE 連結清單 並使用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. 使用 customer_client 資源擷取 ENABLED 帳戶清單。

    SELECT customer_client.id, customer_client.descriptive_name FROM customer_client
    
  3. 兩份清單的差異會顯示已取消的帳戶清單。