Podstawowe użycie

Ten przewodnik zawiera podstawowe informacje o korzystaniu z biblioteki klienta Perl w Google Ads API.

Inicjowanie klienta

Aby zacząć korzystać z biblioteki, utwórz instancję Google::Ads::GoogleAds::Client. Klienta można skonfigurować na kilka sposobów, zgodnie z opisem w przewodniku po konfiguracji. Często stosowanym rozwiązaniem jest użycie pliku googleads.properties:

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

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

Utwórz usługę

Po zainicjowaniu klienta możesz utworzyć instancje różnych usług API. Aby na przykład wchodzić w interakcje z kampaniami, utwórz instancję CampaignService:

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

Wywoływanie interfejsu API

Oto przykład wywołania interfejsu API w celu uzyskania kampanii. Ten fragment kodu pokazuje inicjowanie klienta, tworzenie usługi i wywoływanie metody.

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

Obsługuj błędy

Wywołania interfejsu API mogą powodować wyjątki. Wywołania interfejsu API należy umieszczać w blokach eval, aby przechwytywać i obsługiwać błędy 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";
    }
}

Zobacz inne przykłady

przykładach znajdziesz kilka przydatnych fragmentów kodu. Większość przykładów wymaga parametrów. Możesz przekazywać parametry jako argumenty (zalecane) lub edytować wartości INSERT_XXXXX_HERE w kodzie źródłowym. Aby wyświetlić instrukcję użycia, przekaż -help jako argument wiersza poleceń.

perl examples/basic_operations/get_campaigns.pl -help