במדריך הזה מוסבר על השימוש בספריית הלקוח של 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