ไลบรารีไคลเอ็นต์

ไลบรารีของไคลเอ็นต์ของเรามีมุมมองระดับสูงและองค์ประกอบพื้นฐานของฟังก์ชันการทำงานของ Google Ads API ซึ่งช่วยให้พัฒนาแอปได้อย่างรวดเร็ว เราขอแนะนำให้เริ่มต้นด้วยการใช้ API เดียวหากคุณยังไม่เคยใช้ API

ไลบรารีของไคลเอ็นต์ แหล่งที่มา การจัดจำหน่าย ตัวอย่างโค้ด
Java google-ads-java Maven, tar.gz ดูใน GitHub
.NET google-ads-dotnet nuget, tar.gz, zip ดูใน GitHub
PHP google-ads-php tar.gz ดูใน GitHub
Python google-ads-python tar.gz, zip ดูใน GitHub
Ruby google-ads-ruby gem, tar.gz, zip ดูใน GitHub
Perl google-ads-perl tar.gz, zip ดูใน GitHub

ห้องสมุดชุมชน

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

หากคุณทราบว่ามีห้องสมุดที่เราพลาดไป โปรดคลิกส่งความคิดเห็น ที่ด้านบนของหน้า แล้วแจ้งให้เราทราบ

เวอร์ชัน API ที่รองรับ

ตารางแสดงไลบรารีของไคลเอ็นต์ที่ใช้ได้กับ API เวอร์ชันใด

Java

ไลบรารีของไคลเอ็นต์สำหรับ Java
v21 Min: 39.0.0
Max: -
v20 Min: 38.0.0
Max: -
v19 Min: 36.0.0
Max: -

C#

ไลบรารีของไคลเอ็นต์สำหรับ .NET
v21 Min: 24.0.0
Max: -
v20 Min: 23.0.0
Max: -
v19 Min: 22.1.0
Max: -
v18 Min: 21.1.0
Max: 23.0.0

PHP

ไลบรารีของไคลเอ็นต์สำหรับ PHP
v21 Min: 31.0.0
Max: -
v20 Min: 28.0.0
Max: -
v19 Min: 26.1.0
Max: -
v18 Min: 25.0.0
Max: 30.0.0

Python

ไลบรารีของไคลเอ็นต์สำหรับ Python
v21 Min: 28.0.0
Max: -
v20 Min: 27.0.0
Max: -
v19 Min: 25.2.0
Max: -

Ruby

ไลบรารีของไคลเอ็นต์สำหรับ Ruby
v21 Min: 35.0.0
Max: -
v20 Min: 34.0.0
Max: -
v19 Min: 32.0.0
Max: -
v18 Min: 31.0.0
Max: -

Perl

ไลบรารีของไคลเอ็นต์สำหรับ Perl
v21 Min: 28.0.0
Max: -
v20 Min: 27.0.0
Max: -
v19 Min: 26.0.0
Max: -
v18 Min: 25.0.1
Max: 28.0.0

การกำหนดค่า

ไลบรารีของไคลเอ็นต์ Ads API แต่ละรายการมีการตั้งค่าการกำหนดค่าและ วิธีการโหลดที่แตกต่างกัน ซึ่งคุณสามารถใช้เพื่อปรับแต่งลักษณะการทำงานได้

ต่อไปนี้คือตัวแปรสภาพแวดล้อมที่ใช้กันทั่วไปในไลบรารีของไคลเอ็นต์ทั้งหมดและ ที่โหลดเพื่อตั้งค่าการกำหนดค่าได้

  • ไลบรารีของไคลเอ็นต์
    • GOOGLE_ADS_CONFIGURATION_FILE_PATH: เส้นทางไปยังไฟล์การกำหนดค่า
  • OAuth2
    • โหมดแอปพลิเคชัน
      • GOOGLE_ADS_CLIENT_ID : ตั้งค่านี้เป็นรหัสไคลเอ็นต์ OAuth2
      • GOOGLE_ADS_CLIENT_SECRET : ตั้งค่านี้เป็นข้อมูลลับไคลเอ็นต์ OAuth2
      • GOOGLE_ADS_REFRESH_TOKEN : ตั้งค่านี้เป็นโทเค็นการรีเฟรช OAuth2 ที่สร้างไว้ล่วงหน้า หากต้องการนำโทเค็น OAuth2 กลับมาใช้ซ้ำ การตั้งค่านี้ ไม่บังคับ
    • โหมดบัญชีบริการ
      • GOOGLE_ADS_JSON_KEY_FILE_PATH : ตั้งค่านี้เป็นเส้นทางไฟล์การกำหนดค่า JSON ของ OAuth2
      • GOOGLE_ADS_IMPERSONATED_EMAIL : ตั้งค่านี้เป็นอีเมล ของบัญชีที่คุณแอบอ้าง
  • Google Ads API
    • GOOGLE_ADS_DEVELOPER_TOKEN : ตั้งค่านี้เป็นโทเค็นของนักพัฒนาซอฟต์แวร์
    • GOOGLE_ADS_LOGIN_CUSTOMER_ID : นี่คือรหัสลูกค้าของลูกค้าที่ได้รับอนุญาต ให้ใช้ในคำขอ โดยไม่มีขีดกลาง (-)
    • GOOGLE_ADS_LINKED_CUSTOMER_ID : ส่วนหัวนี้จำเป็นสำหรับเมธอดที่อัปเดตทรัพยากรของเอนทิตีเมื่อได้รับสิทธิ์ผ่านบัญชีที่ลิงก์ใน UI ของ Google Ads (ทรัพยากร AccountLink ใน Google Ads API) เท่านั้น ตั้งค่านี้เป็นรหัสลูกค้าของผู้ให้บริการข้อมูลที่ อัปเดตแหล่งข้อมูลของรหัสลูกค้าที่ระบุ โดยควรตั้งค่า โดยไม่มีขีดกลาง (-) ดูข้อมูลเพิ่มเติมเกี่ยวกับบัญชีที่ลิงก์ได้ที่ ศูนย์ช่วยเหลือ

โดยปกติแล้ว ตัวแปรสภาพแวดล้อมจะกำหนดไว้ในไฟล์การกำหนดค่า Bash เช่น ไฟล์ .bashrc หรือ .bash_profile ที่อยู่ในไดเรกทอรี $HOME และยังกำหนดได้โดยใช้บรรทัดคำสั่งด้วย

ขั้นตอนพื้นฐานในการกำหนดตัวแปรสภาพแวดล้อมโดยใช้ไฟล์ .bashrc โดยใช้เทอร์มินัลมีดังนี้

# Append the line "export GOOGLE_ADS_CLIENT_ID=1234567890" to # the bottom of your .bashrc file. echo "export GOOGLE_ADS_CLIENT_ID=1234567890" >> ~/.bashrc  # Update your bash environment to use the most recently updated # version of your .bashrc file. src ~/.bashrc 

คุณยังตั้งค่าตัวแปรสภาพแวดล้อมในอินสแตนซ์เทอร์มินัลได้โดยตรงจาก บรรทัดคำสั่งด้วย

export GOOGLE_ADS_CLIENT_ID=1234567890 

อีกทางเลือกหนึ่งคือการตั้งค่าตัวแปรสภาพแวดล้อมเมื่อเรียกใช้คำสั่ง ที่ใช้ตัวแปรเหล่านั้น

GOOGLE_ADS_CLIENT_ID=1234567890 php /path/to/script/that/uses/envvar.php 

ดึงข้อมูลเอนทิตี

GoogleAdsService.SearchStream โดยปกติจะใช้เพื่อดึงข้อมูลเอนทิตี และระบบจะแสดงผลลัพธ์เป็นสตรีม ของแถว หรือจะใช้ GoogleAdsService.Search เพื่อดึงข้อมูล เอนทิตีผ่านการเชื่อมต่ออินเทอร์เน็ตที่ไม่เสถียรก็ได้ GoogleAdsService.Search แสดงผลลัพธ์เป็นขนาดหน้าคงที่ 10,000 แถวต่อหน้า

ไลบรารีไคลเอ็นต์ของเราจะใช้การแบ่งหน้าโดยอัตโนมัติ เมื่อคุณทำซ้ำผลลัพธ์เพื่อให้คุณดาวน์โหลดและประมวลผลผลลัพธ์ทั้งหมด พร้อมกันได้ตามลำดับ

Java

private void runExample(GoogleAdsClient googleAdsClient, long customerId) {   try (GoogleAdsServiceClient googleAdsServiceClient =       googleAdsClient.getLatestVersion().createGoogleAdsServiceClient()) {     String query = "SELECT campaign.id, campaign.name FROM campaign ORDER BY campaign.id";     // Constructs the SearchGoogleAdsStreamRequest.     SearchGoogleAdsStreamRequest request =         SearchGoogleAdsStreamRequest.newBuilder()             .setCustomerId(Long.toString(customerId))             .setQuery(query)             .build();      // Creates and issues a search Google Ads stream request that will retrieve all campaigns.     ServerStream<SearchGoogleAdsStreamResponse> stream =         googleAdsServiceClient.searchStreamCallable().call(request);      // Iterates through and prints all of the results in the stream response.     for (SearchGoogleAdsStreamResponse response : stream) {       for (GoogleAdsRow googleAdsRow : response.getResultsList()) {         System.out.printf(             "Campaign with ID %d and name '%s' was found.%n",             googleAdsRow.getCampaign().getId(), googleAdsRow.getCampaign().getName());       }     }   } }       

C#

public void Run(GoogleAdsClient client, long customerId) {     // Get the GoogleAdsService.     GoogleAdsServiceClient googleAdsService = client.GetService(         Services.V21.GoogleAdsService);      // Create a query that will retrieve all campaigns.     string query = @"SELECT                     campaign.id,                     campaign.name,                     campaign.network_settings.target_content_network                 FROM campaign                 ORDER BY campaign.id";      try     {         // Issue a search request.         googleAdsService.SearchStream(customerId.ToString(), query,             delegate (SearchGoogleAdsStreamResponse resp)             {                 foreach (GoogleAdsRow googleAdsRow in resp.Results)                 {                     Console.WriteLine("Campaign with ID {0} and name '{1}' was found.",                         googleAdsRow.Campaign.Id, googleAdsRow.Campaign.Name);                 }             }         );     }     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, int $customerId) {     $googleAdsServiceClient = $googleAdsClient->getGoogleAdsServiceClient();     // Creates a query that retrieves all campaigns.     $query = 'SELECT campaign.id, campaign.name FROM campaign ORDER BY campaign.id';     // Issues a search stream request.     /** @var GoogleAdsServerStreamDecorator $stream */     $stream = $googleAdsServiceClient->searchStream(         SearchGoogleAdsStreamRequest::build($customerId, $query)     );      // Iterates over all rows in all messages and prints the requested field values for     // the campaign in each row.     foreach ($stream->iterateAllElements() as $googleAdsRow) {         /** @var GoogleAdsRow $googleAdsRow */         printf(             "Campaign with ID %d and name '%s' was found.%s",             $googleAdsRow->getCampaign()->getId(),             $googleAdsRow->getCampaign()->getName(),             PHP_EOL         );     } }       

Python

def main(client: GoogleAdsClient, customer_id: str) -> None:     ga_service: GoogleAdsServiceClient = client.get_service("GoogleAdsService")      query: str = """         SELECT           campaign.id,           campaign.name         FROM campaign         ORDER BY campaign.id"""      # Issues a search request using streaming.     stream: Iterator[SearchGoogleAdsStreamResponse] = ga_service.search_stream(         customer_id=customer_id, query=query     )      for batch in stream:         rows: List[GoogleAdsRow] = batch.results         for row in rows:             print(                 f"Campaign with ID {row.campaign.id} and name "                 f'"{row.campaign.name}" was found.'             )       

Ruby

def get_campaigns(customer_id)   # GoogleAdsClient will read a config file from   # ENV['HOME']/google_ads_config.rb when called without parameters   client = Google::Ads::GoogleAds::GoogleAdsClient.new    responses = client.service.google_ads.search_stream(     customer_id: customer_id,     query: 'SELECT campaign.id, campaign.name FROM campaign ORDER BY campaign.id',   )    responses.each do |response|     response.results.each do |row|       puts "Campaign with ID #{row.campaign.id} and name '#{row.campaign.name}' was found."     end   end end       

Perl

sub get_campaigns {   my ($api_client, $customer_id) = @_;    # Create a search Google Ads stream request that will retrieve all campaigns.   my $search_stream_request =     Google::Ads::GoogleAds::V21::Services::GoogleAdsService::SearchGoogleAdsStreamRequest     ->new({       customerId => $customer_id,       query      =>         "SELECT campaign.id, campaign.name FROM campaign ORDER BY campaign.id"     });    # Get the GoogleAdsService.   my $google_ads_service = $api_client->GoogleAdsService();    my $search_stream_handler =     Google::Ads::GoogleAds::Utils::SearchStreamHandler->new({       service => $google_ads_service,       request => $search_stream_request     });    # Issue a search request and process the stream response to print the requested   # field values for the campaign in each row.   $search_stream_handler->process_contents(     sub {       my $google_ads_row = shift;       printf "Campaign with ID %d and name '%s' was found.\n",         $google_ads_row->{campaign}{id}, $google_ads_row->{campaign}{name};     });    return 1; }       

ตัวอย่างโค้ด

ดูตัวอย่างโค้ดของฟังก์ชันทั่วไปบางอย่างใน Google Ads API