基本用法

本指南說明如何使用 Google Ads API 的 Perl 用戶端程式庫。

初始化用戶端

如要開始使用程式庫,請建立 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";
    }
}

查看其他範例

您可以在「examples」中找到幾個實用的程式碼範例。大多數範例都需要參數。您可以將參數做為引數傳遞 (建議),或編輯原始碼中的 INSERT_XXXXX_HERE 值。如要查看範例的使用聲明,請將 -help 做為指令列引數傳遞。

perl examples/basic_operations/get_campaigns.pl -help