इस गाइड में, Google Ads API के लिए Perl क्लाइंट लाइब्रेरी का इस्तेमाल करने के बारे में बुनियादी जानकारी दी गई है.
क्लाइंट को शुरू करना
लाइब्रेरी का इस्तेमाल शुरू करने के लिए, Google::Ads::GoogleAds::Client का एक इंस्टेंस बनाएं. क्लाइंट को कई तरीकों से कॉन्फ़िगर किया जा सकता है. इस बारे में ज़्यादा जानकारी कॉन्फ़िगरेशन गाइड में दी गई है. googleads.properties फ़ाइल का इस्तेमाल करना एक सामान्य तरीका है:
use strict;
use warnings;
use Google::Ads::GoogleAds::Client;
my $api_client = Google::Ads::GoogleAds::Client->new();
कोई सेवा बनाना
क्लाइंट के शुरू होने के बाद, अलग-अलग एपीआई सेवाओं के इंस्टेंस बनाए जा सकते हैं. उदाहरण के लिए, कैंपेन के साथ इंटरैक्ट करने के लिए, आपको CampaignService इंस्टेंस बनाना होगा:
use strict;
use warnings;
my $campaign_service = $api_client->CampaignService();
डिफ़ॉल्ट रूप से, $api_client, इंस्टॉल की गई लाइब्रेरी के DEFAULT_API_VERSION के लिए सेवाएं लोड करता है (V25 सबसे नए वर्शन में; v25.1 जैसे माइनर वर्शन, अपने मेजर वर्शन मॉड्यूल V25 का इस्तेमाल करते हैं). किसी खास एपीआई वर्शन को टारगेट करने के लिए, Client->new({...}) को version => "V25" पास करें या $api_client->set_version("V25") को कॉल करें.
एपीआई कॉल करना
कैंपेन पाने के लिए, एपीआई कॉल करने का तरीका यहां दिया गया है. इस स्निपेट में, क्लाइंट को शुरू करने, सेवा बनाने, और किसी तरीके को कॉल करने का तरीका दिखाया गया है.
use strict;
use warnings;
use Google::Ads::GoogleAds::Client;
use Google::Ads::GoogleAds::V25::Services::GoogleAdsService::SearchGoogleAdsRequest;
# Initialize the Google Ads client. See the Configuration guide for 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::V25::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};
}
गड़बड़ियां ठीक करना
एपीआई कॉल से गड़बड़ियां हो सकती हैं. डिफ़ॉल्ट रूप से, die_on_faults 0 (false) होता है.
इस वजह से, सेवा के तरीके एक चेतावनी जारी करते हैं और एपीआई में गड़बड़ी होने पर, Google::Ads::GoogleAds::GoogleAdsException ऑब्जेक्ट दिखाते हैं. यह देखा जा सकता है कि जवाब में GoogleAdsException शामिल है या नहीं. साथ ही, get_google_ads_failure() को कॉल करके, GoogleAdsError की अलग-अलग एंट्री की जांच की जा सकती है.
इसके अलावा, अगर आपने Client->new({...}) पर die_on_faults => 1 सेट किया है या $api_client->set_die_on_faults(1) को कॉल किया है, तो एपीआई में गड़बड़ी होने पर सेवा के तरीके, die() को रॉ JSON गड़बड़ी वाले रिस्पॉन्स के साथ कॉल करते हैं. इसे eval ब्लॉक (if ($@)) में पकड़ा जा सकता है.
use strict;
use warnings;
use Scalar::Util qw(blessed);
use Google::Ads::GoogleAds::Client;
use Google::Ads::GoogleAds::V25::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::V25::Services::GoogleAdsService::SearchGoogleAdsRequest
->new({
customerId => $customer_id,
query => $query,
});
my $search_response = $google_ads_service->search($search_request);
if (blessed($search_response)
&& $search_response->isa("Google::Ads::GoogleAds::GoogleAdsException"))
{
printf "An API exception occurred (HTTP %s, status '%s'): %s\n",
$search_response->get_code(),
$search_response->get_status(),
$search_response->get_message();
my $google_ads_failure = $search_response->get_google_ads_failure();
if ($google_ads_failure) {
foreach my $error (@{$google_ads_failure->{errors} || []}) {
my ($error_type, $error_value) = %{$error->{errorCode} || {}};
printf " Error code (%s): %s\n",
$error_type || "unknown",
$error_value || "UNKNOWN";
printf " Message: %s\n", $error->{message};
}
}
} else {
foreach my $google_ads_row (@{$search_response->{results} || []}) {
printf "Campaign name: %s\n", $google_ads_row->{campaign}->{name};
}
}
अन्य उदाहरण देखें
आपको examples
directory में, काम के कई कोड सैंपल मिल सकते हैं. ज़्यादातर उदाहरणों के लिए पैरामीटर ज़रूरी होते हैं. पैरामीटर को आर्ग्युमेंट के तौर पर पास किया जा सकता है (सुझाया गया है) या सोर्स कोड में INSERT_XXXXX_HERE वैल्यू में बदलाव किया जा सकता है. इस्तेमाल से जुड़ा स्टेटमेंट देखने के लिए, -help को कमांड-लाइन आर्ग्युमेंट के तौर पर पास करें.
perl examples/basic_operations/get_campaigns.pl -help