คู่มือนี้อธิบายพื้นฐานของการใช้ไลบรารีของไคลเอ็นต์ Perl สำหรับ Google Ads API
เริ่มต้นไคลเอ็นต์
หากต้องการเริ่มใช้ไลบรารี ให้สร้างอินสแตนซ์ของ
Google::Ads::GoogleAds::Client คุณกำหนดค่าไคลเอ็นต์ได้หลายวิธี
ตามที่ระบุไว้ในคู่มือการกำหนดค่า แนวทางที่ใช้กันโดยทั่วไปคือการ
ใช้ไฟล์ googleads.properties ดังนี้
use Google::Ads::GoogleAds::Client;
my $api_client = Google::Ads::GoogleAds::Client->new();
สร้างบริการ
เมื่อเริ่มต้นไคลเอ็นต์แล้ว คุณจะสร้างอินสแตนซ์ของบริการ API ต่างๆ ได้
เช่น หากต้องการโต้ตอบกับแคมเปญ คุณจะต้องสร้างอินสแตนซ์
CampaignService
my $campaign_service = $api_client->CampaignService();
ทำการเรียก API
ต่อไปนี้เป็นตัวอย่างวิธีเรียก API เพื่อรับแคมเปญ ข้อมูลโค้ดนี้ แสดงการเริ่มต้นไคลเอ็นต์ การสร้างบริการ และการเรียกใช้เมธอด
use strict;
use warnings;
use Google::Ads::GoogleAds::Client;
use Google::Ads::GoogleAds::V22::Services::GoogleAdsService::SearchGoogleAdsRequest;
# Initialize the Google Ads client. See the [Configuration guide](configuration) for more options.
my $api_client = Google::Ads::GoogleAds::Client->new();
# Get the GoogleAdsService.
my $google_ads_service = $api_client->GoogleAdsService();
# Specify the customer ID and query.
my $customer_id = "INSERT_CUSTOMER_ID_HERE";
my $query = "SELECT campaign.name, campaign.status FROM campaign";
# Create a search request.
my $search_request = Google::Ads::GoogleAds::V22::Services::GoogleAdsService::SearchGoogleAdsRequest
->new({
customerId => $customer_id,
query => $query
});
# Execute the search request.
my $search_response = $google_ads_service->search($search_request);
# Iterate over the results and print campaign names.
foreach my $google_ads_row (@{$search_response->{results}}) {
printf "Campaign with resource name '%s' and name '%s'.\n",
$google_ads_row->{campaign}->{resourceName},
$google_ads_row->{campaign}->{name};
}
จัดการข้อผิดพลาด
การเรียก API อาจทำให้เกิดข้อยกเว้น คุณควรห่อหุ้มการเรียก API ในบล็อก eval
เพื่อตรวจหาและจัดการ Google::Ads::GoogleAds::GoogleAdsException
use strict;
use warnings;
use Google::Ads::GoogleAds::Client;
use Google::Ads::GoogleAds::GoogleAdsException;
use Google::Ads::GoogleAds::V22::Services::GoogleAdsService::SearchGoogleAdsRequest;
my $api_client = Google::Ads::GoogleAds::Client->new();
my $google_ads_service = $api_client->GoogleAdsService();
my $customer_id = "INSERT_CUSTOMER_ID_HERE";
my $query = "SELECT campaign.name, campaign.status FROM campaign";
my $search_request = Google::Ads::GoogleAds::V22::Services::GoogleAdsService::SearchGoogleAdsRequest->new({
customerId => $customer_id,
query => $query
});
eval {
my $search_response = $google_ads_service->search($search_request);
# Process successful response.
foreach my $google_ads_row (@{$search_response->{results}}) {
printf "Campaign name: %s\n", $google_ads_row->{campaign}->{name};
}
};
if ($@) {
if (blessed($@) && $@->isa('Google::Ads::GoogleAds::GoogleAdsException')) {
my $exception = $@;
printf "A Google Ads exception occurred:\n";
printf " Request ID: %s\n", $exception->get_request_id();
foreach my $error (@{$exception->get_errors()}) {
printf " Error code: %s\n", $error->{errorCode};
printf " Message: %s\n", $error->{message};
}
} else {
# Print other types of exceptions.
print "An error occurred: $@\n";
}
}
ดูตัวอย่างอื่นๆ
คุณดูตัวอย่างโค้ดที่มีประโยชน์ได้หลายรายการในตัวอย่าง
ตัวอย่างส่วนใหญ่ต้องใช้พารามิเตอร์ คุณจะส่งพารามิเตอร์เป็นอาร์กิวเมนต์ (แนะนํา) หรือแก้ไขค่า INSERT_XXXXX_HERE ในซอร์สโค้ดก็ได้ หากต้องการดูคำสั่งการใช้งานสำหรับตัวอย่าง ให้ส่ง -help เป็นอาร์กิวเมนต์บรรทัดคำสั่ง
perl examples/basic_operations/get_campaigns.pl -help