इस गाइड में, Google Ads API के लिए Perl क्लाइंट लाइब्रेरी का इस्तेमाल करने के बारे में बुनियादी जानकारी दी गई है.
क्लाइंट को शुरू करना
लाइब्रेरी का इस्तेमाल शुरू करने के लिए, Google::Ads::GoogleAds::Client का एक इंस्टेंस बनाएं. क्लाइंट को कई तरीकों से कॉन्फ़िगर किया जा सकता है. इस बारे में ज़्यादा जानकारी कॉन्फ़िगरेशन गाइड में दी गई है. googleads.properties फ़ाइल का इस्तेमाल करना, एक सामान्य तरीका है:
use Google::Ads::GoogleAds::Client;
my $api_client = Google::Ads::GoogleAds::Client->new();
कोई सेवा बनाना
क्लाइंट के शुरू होने के बाद, अलग-अलग एपीआई सेवाओं के इंस्टेंस बनाए जा सकते हैं. उदाहरण के लिए, कैंपेन के साथ इंटरैक्ट करने के लिए, आपको CampaignService इंस्टेंस बनाना होगा:
my $campaign_service = $api_client->CampaignService();
एपीआई कॉल करना
कैंपेन पाने के लिए, एपीआई कॉल करने का तरीका यहां दिया गया है. इस स्निपेट में, क्लाइंट को शुरू करने, सेवा बनाने, और किसी तरीके को कॉल करने का तरीका दिखाया गया है.
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};
}
गड़बड़ियां ठीक करना
एपीआई कॉल से अपवाद हो सकते हैं. आपको अपने एपीआई कॉल को 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";
}
}
अन्य उदाहरण देखें
आपको examples में कई काम के कोड सैंपल मिल सकते हैं.
ज़्यादातर उदाहरणों के लिए पैरामीटर ज़रूरी होते हैं. पैरामीटर को आर्ग्युमेंट के तौर पर पास किया जा सकता है (सुझाया गया है) या सोर्स कोड में INSERT_XXXXX_HERE की वैल्यू में बदलाव किया जा सकता है. इस्तेमाल से जुड़ा स्टेटमेंट देखने के लिए, -help को कमांड-लाइन
आर्ग्युमेंट के तौर पर पास करें.
perl examples/basic_operations/get_campaigns.pl -help