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

ไลบรารีของไคลเอ็นต์แสดงมุมมองระดับสูงและองค์ประกอบพื้นฐานของฟังก์ชันการทำงานของ Google Ads API ซึ่งช่วยให้พัฒนาแอปได้อย่างรวดเร็ว เราขอแนะนําให้เริ่มต้นด้วย 1 รายการหากคุณเพิ่งเริ่มใช้ 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
v19 Min: 36.0.0
Max: -
v18 Min: 34.0.0
Max: -
v17 Min: 32.0.0
Max: -

C#

ไลบรารีของไคลเอ็นต์สำหรับ .NET
v19 Min: 22.1.0
Max: -
v18 Min: 21.1.0
Max: -
v17 Min: 20.1.0
Max: -
v16 Min: 18.1.0
Max: 21.1.0

PHP

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

Python

ไลบรารีของไคลเอ็นต์สำหรับ Python
v19 Min: 25.2.0
Max: -
v18 Min: 25.1.0
Max: -
v17 Min: 24.1.0
Max: -
v16 Min: 23.1.0
Max: -

Ruby

ไลบรารีของไคลเอ็นต์สําหรับ Ruby
v19 Min: 32.0.0
Max: -
v18 Min: 31.0.0
Max: -
v17 Min: 29.0.0
Max: -

Perl

ไลบรารีของไคลเอ็นต์สําหรับ Perl
v19 Min: 26.0.0
Max: -
v18 Min: 25.0.1
Max: -
v17 Min: 23.0.0
Max: -

การกำหนดค่า

ไลบรารีไคลเอ็นต์ 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.V19.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, customer_id):     ga_service = client.get_service("GoogleAdsService")      query = """         SELECT           campaign.id,           campaign.name         FROM campaign         ORDER BY campaign.id"""      # Issues a search request using streaming.     stream = ga_service.search_stream(customer_id=customer_id, query=query)      for batch in stream:         for row in batch.results:             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::V19::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