このガイドでは、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