Cách sử dụng cơ bản

Hướng dẫn này giải thích những điều cơ bản về cách sử dụng thư viện ứng dụng Perl cho API Google Ads.

Khởi động ứng dụng

Để bắt đầu sử dụng thư viện, hãy tạo một thực thể của Google::Ads::GoogleAds::Client. Bạn có thể định cấu hình ứng dụng theo nhiều cách, như được trình bày chi tiết trong Hướng dẫn định cấu hình. Một phương pháp phổ biến là sử dụng tệp googleads.properties:

use Google::Ads::GoogleAds::Client;

my $api_client = Google::Ads::GoogleAds::Client->new();

Tạo một dịch vụ

Sau khi khởi tạo ứng dụng, bạn có thể tạo các phiên bản của nhiều dịch vụ API. Ví dụ: để tương tác với các chiến dịch, bạn sẽ tạo một phiên bản CampaignService:

my $campaign_service = $api_client->CampaignService();

Thực hiện lệnh gọi API

Sau đây là ví dụ về cách thực hiện lệnh gọi API để nhận chiến dịch. Đoạn mã này minh hoạ cách khởi chạy ứng dụng, tạo một dịch vụ và gọi một phương thức.

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};
}

Xử lý lỗi

Các lệnh gọi API có thể dẫn đến ngoại lệ. Bạn nên gói các lệnh gọi API trong các khối eval để nắm bắt và xử lý 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";
    }
}

Khám phá các ví dụ khác

Bạn có thể tìm thấy một số mẫu mã hữu ích trong examples. Hầu hết các ví dụ đều yêu cầu tham số. Bạn có thể truyền các tham số dưới dạng đối số (nên dùng) hoặc chỉnh sửa các giá trị INSERT_XXXXX_HERE trong mã nguồn. Để xem nội dung sử dụng cho một ví dụ, hãy truyền -help làm đối số dòng lệnh.

perl examples/basic_operations/get_campaigns.pl -help