您可以使用
ListAccessibleCustomers
方法CustomerService
。不过,有必要了解一下此类请求会返回哪些客户。
列出可访问的客户是 Google Ads API 中实现
不需要您在请求中指定客户 ID,并且会忽略
提供的 login-customer-id
。
生成的客户列表取决于您的 OAuth 凭据。该请求会返回一个列表,其中会列出您可以根据当前凭据直接采取操作的所有账号。它不一定包括账号层次结构中的所有账号,只会包括已通过账号中的管理员权限或其他权限添加了身份验证成功用户的账号。
假设您是用户 A
,同时是上图两个层次结构中 M1
和 C3
的管理员。如果您要调用 Google Ads API,例如
GoogleAdsService
,您可以使用
M1
、C1
、C2
、
和 C3
。不过,对
CustomerService.ListAccessibleCustomers
会
仅返回 M1
和 C3
,因为只有这两个账号是用户 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}"')
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; }
列出已撤销的账号
Google Ads API 无法直接提供在以下位置列出已撤销的账号 经理账号。不过,您可以使用以下解决方法来检索 此列表。
使用
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
使用
customer_client
资源检索ENABLED
账号的列表。SELECT customer_client.id, customer_client.descriptive_name FROM customer_client
通过区分这两个列表,您可以找到已撤销账号的列表。