ทําความเข้าใจรูปแบบการเข้าถึง Google Ads

บัญชี Google Ads มี 2 ประเภท ได้แก่ บัญชีดูแลจัดการ Google Ads และบัญชีผู้ลงโฆษณา Google Ads ดูคำอธิบายโดยละเอียดเกี่ยวกับความสัมพันธ์ของคำเหล่านี้และคำอื่นๆ (เช่น บัญชีลูกค้าและลูกค้า) ได้ที่ประเภทบัญชี

บัญชีดูแลจัดการสามารถจัดการบัญชีดูแลจัดการ Google Ads อื่นๆ หรือบัญชีผู้ลงโฆษณา Google Ads ได้ คุณสามารถลิงก์บัญชีผู้ลงโฆษณากับบัญชีดูแลจัดการ แล้วจัดการบัญชีผู้ลงโฆษณาผ่านบัญชีดูแลจัดการได้ โครงสร้างที่ลิงก์โดยรวมคือกราฟแบบมีทิศทางและไม่มีวงจรที่มีบัญชีผู้ลงโฆษณาอยู่ที่ระดับ Leaf

คุณสามารถให้สิทธิ์เข้าถึงบัญชี Google Ads แก่ผู้ใช้หรือบัญชีบริการแต่ละรายได้ คุณให้สิทธิ์ผู้ใช้เข้าถึงบัญชีผู้ลงโฆษณาได้ 2 วิธีดังนี้

  • ให้สิทธิ์เข้าถึงบัญชีผู้ลงโฆษณาโดยตรงแก่ผู้ใช้โดยเชิญผู้ใช้เข้าสู่บัญชีนั้น
  • ให้สิทธิ์เข้าถึงบัญชีผู้ลงโฆษณาแก่ผู้ใช้โดยอ้อมด้วยการเชิญผู้ใช้เข้าสู่บัญชีดูแลจัดการที่ลิงก์กับบัญชีนั้น ผู้ใช้จะได้รับสิทธิ์เข้าถึงบัญชีผู้ลงโฆษณาเนื่องจากบัญชีดูแลจัดการมีสิทธิ์เข้าถึงบัญชีทั้งหมดที่ลิงก์อยู่ภายใต้บัญชีดังกล่าว

นอกจากนี้ คุณยังมอบหมายบทบาทของผู้ใช้ได้เมื่อเชิญผู้ใช้ให้จัดการบัญชี

ลองพิจารณาลำดับชั้นบัญชีต่อไปนี้ สมมติว่าผู้ใช้ทั้งหมดมีสิทธิ์การเข้าถึงระดับมาตรฐาน

แผนภาพแสดงบัญชีดูแลจัดการ M1, M2 และ M3 ที่ลิงก์กับบัญชีผู้ลงโฆษณา A1 ถึง A4

ตารางต่อไปนี้สรุปโครงสร้างบัญชีนี้

ผู้ใช้ มีสิทธิ์เข้าถึงโดยตรง มีสิทธิ์เข้าถึงทางอ้อมใน
U1, SA1 M1 M2, A1, A2, A3
U2 M2, M3 A1, A2, A3, A4
U3 A4  

รหัสลูกค้าที่เข้าสู่ระบบ

ผู้ใช้อาจมีสิทธิ์เข้าถึงลำดับชั้นของบัญชีหลายรายการ เมื่อทำการเรียก API ในกรณีดังกล่าว คุณต้องระบุบัญชีรูทที่จะใช้เพื่อกำหนดระดับการให้สิทธิ์และการเข้าถึงบัญชีอย่างถูกต้อง โดยให้ใส่ส่วนหัว login-customer-id ในคำขอ API

ตารางต่อไปนี้ใช้ลำดับชั้นของบัญชีจากตัวอย่างก่อนหน้าเพื่อแสดงรหัสลูกค้าที่ใช้เข้าสู่ระบบซึ่งคุณใช้ได้ และรายการบัญชีที่เกี่ยวข้องซึ่งคุณสามารถเรียกใช้ได้

ผู้ใช้ รหัสลูกค้าที่ใช้เข้าสู่ระบบ บัญชีที่จะทำการเรียก API
U1, SA1 M1 M1, M2, A1, A2, A3
U2 M2 M2, A1, A2, A3
U2 M3 M3, A1, A4
U3 A4 A4

คุณสามารถข้ามการระบุส่วนหัว login-customer-id ได้หากผู้ใช้มีสิทธิ์เข้าถึงบัญชี Google Ads ที่คุณกำลังเรียกใช้โดยตรง เช่น คุณไม่จำเป็นต้องระบุส่วนหัว login-customer-id เมื่อใช้ข้อมูลเข้าสู่ระบบ U3 เพื่อเรียกใช้ A4 เนื่องจากเซิร์ฟเวอร์ Google Ads สามารถระบุระดับการเข้าถึงจากรหัสลูกค้า (A4) ได้อย่างถูกต้อง

หากคุณใช้ไลบรารีของไคลเอ็นต์ Google Ads API ให้ใช้การตั้งค่าต่อไปนี้เพื่อระบุส่วนหัว login-customer-id

Java

เพิ่มการตั้งค่าต่อไปนี้ลงในไฟล์ ads.properties

api.googleads.loginCustomerId=INSERT_LOGIN_CUSTOMER_ID_HERE

.NET

เพิ่มการตั้งค่าต่อไปนี้เมื่อเริ่มต้นออบเจ็กต์ GoogleAdsConfig และใช้เพื่อสร้างออบเจ็กต์ GoogleAdsClient

GoogleAdsConfig config = new GoogleAdsConfig()
{
    // ...
    LoginCustomerId = "INSERT_LOGIN_CUSTOMER_ID_HERE"
};
GoogleAdsClient client = new GoogleAdsClient(config);

Python

เพิ่มการตั้งค่าต่อไปนี้ลงในไฟล์ google-ads.yaml

login_customer_id: INSERT_LOGIN_CUSTOMER_ID_HERE

PHP

เพิ่มการตั้งค่าต่อไปนี้ลงในไฟล์ google_ads_php.ini

[GOOGLE_ADS]
loginCustomerId = "INSERT_LOGIN_CUSTOMER_ID_HERE"

Ruby

เพิ่มการตั้งค่าต่อไปนี้ลงในไฟล์ google_ads_config.rb

Google::Ads::GoogleAds::Config.new do |c|
  c.login_customer_id = 'INSERT_LOGIN_CUSTOMER_ID_HERE'
end

สร้างอินสแตนซ์ GoogleAdsClient โดยส่งเส้นทางไปยังตำแหน่งที่คุณเก็บไฟล์นี้

client = Google::Ads::GoogleAds::GoogleAdsClient.new(
  'path/to/google_ads_config.rb'
)

Perl

เพิ่มการตั้งค่าต่อไปนี้ลงในไฟล์ googleads.properties

loginCustomerId=INSERT_LOGIN_CUSTOMER_ID_HERE

curl

ระบุอาร์กิวเมนต์บรรทัดคำสั่งต่อไปนี้เมื่อเรียกใช้คำสั่ง curl

-H "login-customer-id: INSERT_LOGIN_CUSTOMER_ID_HERE"

คุณสามารถใช้เมธอด CustomerService.ListAccessibleCustomers เพื่อดึงข้อมูลรายการบัญชีที่ผู้ใช้มีสิทธิ์เข้าถึงโดยตรง บัญชีเหล่านี้สามารถใช้เป็นค่าที่ถูกต้องสำหรับส่วนหัว login-customer-id

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.V26.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: GoogleAdsClient) -> None:
    customer_service: CustomerServiceClient = client.get_service(
        "CustomerService"
    )

    accessible_customers: ListAccessibleCustomersResponse = (
        customer_service.list_accessible_customers()
    )
    result_total: int = len(accessible_customers.resource_names)
    print(f"Total results: {result_total}")

    resource_names: List[str] = accessible_customers.resource_names
    for resource_name in resource_names:  # resource_name is implicitly str
        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;
}
      

curl

# Returns the resource names of customers directly accessible by the user
# authenticating the call.
#
# Variables:
#   API_VERSION,
#   OAUTH2_ACCESS_TOKEN:
#     See https://developers.google.com/google-ads/api/rest/auth#request_headers
#     for details.
#
curl -f --request GET \
"https://googleads.googleapis.com/v${API_VERSION}/customers:listAccessibleCustomers" \
--header "Content-Type: application/json" \
--header "Authorization: Bearer ${OAUTH2_ACCESS_TOKEN}" \
      

ฉันควรทำอย่างไรหากเมธอด ListAccessibleCustomers ไม่ดึงข้อมูลรหัสลูกค้า

หากเมธอด CustomerService.ListAccessibleCustomers ไม่ได้ดึงรหัสลูกค้าที่คุณคาดว่าจะปรากฏในผลลัพธ์ อาจมีคำอธิบายที่เป็นไปได้ 2-3 ข้อ

  1. คุณมีสิทธิ์เข้าถึงรหัสลูกค้า แต่สิทธิ์เข้าถึงดังกล่าวออกผ่านบัญชีดูแลจัดการหลัก ตัวอย่างเช่น หากคุณเรียกใช้เมธอด ListAccessibleCustomers ด้วยข้อมูลเข้าสู่ระบบของผู้ใช้ U1 ในตัวอย่างก่อนหน้า เมธอดจะแสดงผลเฉพาะ M1 ในผลลัพธ์ แม้ว่า U1 จะมีสิทธิ์เข้าถึงบัญชีอื่นๆ ก็ตาม หากต้องการยืนยันความเป็นไปได้นี้ ให้ดึงข้อมูลลำดับชั้นของบัญชีโดยเรียกใช้การค้นหา search หรือ searchStream กับทรัพยากร customer_client สำหรับแต่ละบัญชีที่แสดงผลโดยเมธอด ListAccessibleCustomers โดยตั้งค่าแต่ละบัญชีเหล่านี้เป็นlogin-customer-idตามที่อธิบายไว้ในส่วนก่อนหน้า หากมีสิทธิ์เข้าถึงบัญชีเป้าหมาย คุณจะดึงข้อมูลบัญชีดังกล่าวเป็นส่วนหนึ่งของลำดับชั้นบัญชีได้

  2. คุณใช้ข้อมูลเข้าสู่ระบบ OAuth ที่ไม่ถูกต้อง สถานการณ์ที่พบบ่อยที่สุดคือ คุณกำลังใช้ข้อมูลเข้าสู่ระบบของผู้ใช้รายอื่น ตัวอย่างเช่น อาจเกิดจากการผสมข้อมูลเข้าสู่ระบบของแซนด์บ็อกซ์หรือนักพัฒนาแอปกับข้อมูลเข้าสู่ระบบเวอร์ชันที่ใช้งานจริงโดยไม่ตั้งใจ หรือการอ่านข้อมูลเข้าสู่ระบบของผู้ใช้รายอื่นจากฐานข้อมูลหรือแคชในเครื่องอย่างไม่ถูกต้อง วิธีหนึ่งที่เป็นไปได้ในการแก้ปัญหานี้คือการใช้ Google People API เพื่อดึงชื่อและอีเมลของผู้ใช้ที่เข้าสู่ระบบ และยืนยันว่าตรงกับอีเมลที่คุณคาดไว้หรือไม่

  3. คุณไม่มีสิทธิ์เข้าถึงบัญชี ทำตามคู่มือการเข้าถึงบัญชีเพื่อรับสิทธิ์เข้าถึงบัญชีลูกค้าที่ถูกต้อง

บทบาทของผู้ใช้

Google Ads API ไม่มีโมเดลการเข้าถึงแยกต่างหากของตัวเอง หรือใช้ขอบเขต OAuth 2.0 แยกต่างหากเพื่อจำกัดฟังก์ชันการทำงาน เช่น Google Ads API ใช้ขอบเขตเดียวกันสำหรับการดำเนินการแบบอ่านอย่างเดียวกับการดำเนินการแบบอ่านและเขียน แต่ Google Ads API จะใช้บทบาทของผู้ใช้เดียวกันกับที่ Google Ads รองรับ เมื่อมีการให้บทบาทของผู้ใช้แก่บัญชีในระดับบัญชีดูแลจัดการ บัญชีในลำดับชั้นจะรับบทบาทดังกล่าวมาด้วย หากผู้ใช้มีบทบาทที่ขัดแย้งกันในบัญชีหนึ่งๆ ระดับที่ถูกต้องจะได้รับการแก้ไขโดยบัญชี login-customer-id ที่ระบุไว้ในคำขอ API

ตารางต่อไปนี้ใช้ลำดับชั้นของบัญชีจากตัวอย่างก่อนหน้า และแสดงผลของการให้บทบาทต่างๆ แก่ผู้ใช้

ผู้ใช้ ให้บทบาทผู้ใช้แล้ว login-customer-id ระดับการเข้าถึงที่มีผล
SA1 สิทธิ์การเข้าถึงระดับมาตรฐานในบัญชี M1 M1 สิทธิ์การเข้าถึงระดับมาตรฐานใน M1, M2, A1, A2, A3
U2 สิทธิ์การเข้าถึงระดับมาตรฐานใน M2
สิทธิ์การเข้าถึงระดับอ่านอย่างเดียวใน M3
M2 สิทธิ์การเข้าถึงระดับมาตรฐานใน M2, A1, A2, A3
U2 สิทธิ์การเข้าถึงระดับมาตรฐานใน M2
สิทธิ์การเข้าถึงระดับอ่านอย่างเดียวใน M3
M3 สิทธิ์การเข้าถึงระดับอ่านอย่างเดียวใน M3, A1, A4