فهرست حساب های قابل دسترسی

می‌توانید مشتریانی را که در دسترس شما هستند با روش ListAccessibleCustomers در CustomerService فهرست کنید. با این حال، باید درک کرد که چه مشتریانی در این نوع درخواست بازگردانده می شوند.

فهرست کردن مشتریان قابل دسترس یکی از معدود درخواست‌هایی در Google Ads API است که نیازی به تعیین شناسه مشتری در درخواست ندارد و هر login-customer-id ارائه شده را نادیده می‌گیرد.

لیست مشتریان حاصل بر اساس اعتبارنامه OAuth شما است. این درخواست فهرستی از تمام حساب‌هایی را که می‌توانید با توجه به اعتبار فعلی‌تان مستقیماً روی آنها عمل کنید، بازمی‌گرداند. این لزوماً شامل همه حساب‌های موجود در سلسله مراتب حساب نمی‌شود. بلکه فقط شامل حساب‌هایی می‌شود که کاربر تأیید شده شما با ادمین یا سایر حقوق در حساب اضافه شده است.

تصور کنید کاربر A هستید که در دو سلسله مراتب تصویر بالا مدیر M1 و C3 است. اگر می‌خواهید با Google Ads API تماس بگیرید، برای مثال با GoogleAdsService ، می‌توانید به اطلاعات حساب‌های M1 ، C1 ، C2 و C3 دسترسی داشته باشید. با این حال، تماس با CustomerService.ListAccessibleCustomers فقط M1 و C3 را برمی گرداند زیرا این حساب ها تنها حساب هایی هستند که کاربر A دسترسی مستقیم دارد.

در اینجا یک نمونه کد نشان دهنده استفاده از روش CustomerService.ListAccessibleCustomers است:

جاوا

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

سی شارپ

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

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

پایتون

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
      

پرل

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. لیست پیوندهای ACTIVE را با استفاده از منبع customer_client_link بازیابی کنید و با استفاده از قسمت 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. تفاوت بین این دو لیست لیستی از حساب های لغو شده را به شما می دهد.