기본 사용법

이 가이드에서는 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 호출로 인해 예외가 발생할 수 있습니다. Google::Ads::GoogleAds::GoogleAdsException를 포착하고 처리하려면 API 호출을 eval 블록으로 래핑해야 합니다.

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