Nasze biblioteki klienta udostępniają widoki wysokiego poziomu i podstawowe elementy funkcjonalności interfejsu Google Ads API, co ułatwia szybkie tworzenie aplikacji. Jeśli dopiero zaczynasz korzystać z interfejsu API, zalecamy rozpoczęcie od jednego z nich.
| Biblioteka klienta | Źródło | Dystrybucja | Przykłady kodu |
|---|---|---|---|
| Java | google-ads-java | Maven, tar.gz | Wyświetl w GitHubie |
| .NET | google-ads-dotnet | nuget, tar.gz, zip | Wyświetl w GitHubie |
| PHP | google-ads-php | tar.gz | Wyświetl w GitHubie |
| Python | google-ads-python | tar.gz, zip | Wyświetl w GitHubie |
| Ruby | google-ads-ruby | gem, tar.gz, zip | Wyświetl w GitHubie |
| Perl | google-ads-perl | tar.gz, zip | Wyświetl w GitHubie |
Biblioteki społecznościowe
Znamy kilka bibliotek, które są utrzymywane przez społeczność open source. Prowadzimy tę listę, aby ułatwić zainteresowanym deweloperom znalezienie tych usług. Nie testujemy tych bibliotek, nie współtworzymy ich ani nie zarządzamy nimi. Korzystasz z nich na własne ryzyko. Wszelkie pytania należy kierować do właścicieli projektów open source.
Jeśli znasz bibliotekę, której nie uwzględniliśmy, kliknij Prześlij opinię u góry strony i daj nam znać.
Obsługiwane wersje interfejsu API
Tabela pokazuje, które biblioteki klienta działają z którymi wersjami interfejsu API.
Java
Interfejs Google Ads API |
Biblioteka klienta Java |
v22 |
Min: 41.1.0Max: - |
v21 |
Min: 39.0.0Max: - |
v20 |
Min: 38.0.0Max: - |
v19 |
Min: 36.0.0Max: - |
C#
Interfejs Google Ads API |
Biblioteka klienta dla .NET |
v22 |
Min: 24.1.0Max: - |
v21 |
Min: 24.0.0Max: - |
v20 |
Min: 23.0.0Max: - |
v19 |
Min: 22.1.0Max: - |
PHP
Interfejs Google Ads API |
Biblioteka klienta PHP |
v22 |
Min: 31.0.0Max: - |
v21 |
Min: 28.0.0Max: - |
v20 |
Min: 26.1.0Max: - |
v19 |
Min: 25.0.0Max: 30.0.0 |
Python
Interfejs Google Ads API |
Biblioteka klienta w Pythonie |
v22 |
Min: 28.1.0Max: - |
v21 |
Min: 28.0.0Max: - |
v20 |
Min: 27.0.0Max: - |
v19 |
Min: 25.2.0Max: - |
Ruby
Interfejs Google Ads API |
Biblioteka klienta Ruby |
v22 |
Min: 36.0.0Max: - |
v21 |
Min: 35.0.0Max: - |
v20 |
Min: 34.0.0Max: - |
v19 |
Min: 32.0.0Max: - |
Perl
Interfejs Google Ads API |
Biblioteka klienta Perl |
v22 |
Min: 29.0.0Max: - |
v21 |
Min: 28.0.0Max: - |
v20 |
Min: 27.0.0Max: - |
v19 |
Min: 26.0.0Max: - |
Konfiguracja
Każda biblioteka klienta interfejsu Google Ads API udostępnia różne ustawienia konfiguracji i metody wczytywania, których możesz użyć do dostosowania jej działania.
Oto zmienne środowiskowe, które są wspólne dla wszystkich bibliotek klienta i które można wczytać, aby ustawić opcje konfiguracji:
- Biblioteka klienta
GOOGLE_ADS_CONFIGURATION_FILE_PATH: Ścieżka do pliku konfiguracyjnego.
- OAuth2
- Tryb aplikacji
GOOGLE_ADS_CLIENT_ID: ustaw tę wartość na identyfikator klienta OAuth2.GOOGLE_ADS_CLIENT_SECRET: ustaw tę wartość na klucz tajny klienta OAuth2.GOOGLE_ADS_REFRESH_TOKEN: jeśli chcesz ponownie użyć tokenów OAuth2, ustaw tę wartość na wcześniej wygenerowany token odświeżania OAuth2. To ustawienie jest opcjonalne.
- Tryb konta usługi
GOOGLE_ADS_JSON_KEY_FILE_PATH: ustaw tę wartość na ścieżkę do pliku konfiguracyjnego JSON OAuth2.GOOGLE_ADS_IMPERSONATED_EMAIL: ustaw tę wartość na adres e-mail konta, które chcesz reprezentować.
- Tryb aplikacji
- Interfejs Google Ads API
GOOGLE_ADS_DEVELOPER_TOKEN: ustaw na token programisty.GOOGLE_ADS_LOGIN_CUSTOMER_ID: identyfikator klienta uprawnionego do używania w żądaniu, bez łączników (-).GOOGLE_ADS_LINKED_CUSTOMER_ID: ten nagłówek jest wymagany tylko w przypadku metod, które aktualizują zasoby jednostki, gdy uprawnienia są przyznawane za pomocą połączonych kont w interfejsie Google Ads (zasóbAccountLinkw interfejsie Google Ads API). Ustaw tę wartość na identyfikator klienta dostawcy danych, który aktualizuje zasoby określonego identyfikatora klienta. Należy go podać bez łączników (-). Więcej informacji o połączonych kontach znajdziesz w Centrum pomocy.
Zmienne środowiskowe są zwykle zdefiniowane w pliku konfiguracyjnym bash, np. w pliku .bashrc lub .bash_profile znajdującym się w katalogu $HOME. Można je też zdefiniować za pomocą wiersza poleceń.
Oto podstawowe czynności, które należy wykonać, aby zdefiniować zmienną środowiskową za pomocą pliku .bashrc
w terminalu:
# Append the line "export GOOGLE_ADS_CLIENT_ID=1234567890" to
# the bottom of your .bashrc file.
echo "export GOOGLE_ADS_CLIENT_ID=1234567890" >> ~/.bashrc
# Update your bash environment to use the most recently updated
# version of your .bashrc file.
src ~/.bashrc
Zmienne środowiskowe można też ustawić w instancji terminala bezpośrednio z wiersza poleceń:
export GOOGLE_ADS_CLIENT_ID=1234567890
Inną opcją jest ustawienie zmiennych środowiskowych podczas wywoływania polecenia, które ich używa:
GOOGLE_ADS_CLIENT_ID=1234567890 php /path/to/script/that/uses/envvar.php
Pobieranie elementów
GoogleAdsService.SearchStream
służy zwykle do pobierania encji, a wyniki są zwracane jako strumień wierszy. Możesz też użyć
GoogleAdsService.Search, aby pobrać
jednostki przez niestabilne połączenie internetowe. GoogleAdsService.Search
zwraca wyniki w postaci stron o stałym rozmiarze 10 tys. wierszy na stronę.
Nasza biblioteka klienta automatycznie wdraża stronicowanie podczas iteracji wyników, dzięki czemu możesz pobierać i przetwarzać je sekwencyjnie w jednym kroku.
Java
private void runExample(GoogleAdsClient googleAdsClient, long customerId) { try (GoogleAdsServiceClient googleAdsServiceClient = googleAdsClient.getLatestVersion().createGoogleAdsServiceClient()) { String query = "SELECT campaign.id, campaign.name FROM campaign ORDER BY campaign.id"; // Constructs the SearchGoogleAdsStreamRequest. SearchGoogleAdsStreamRequest request = SearchGoogleAdsStreamRequest.newBuilder() .setCustomerId(Long.toString(customerId)) .setQuery(query) .build(); // Creates and issues a search Google Ads stream request that will retrieve all campaigns. ServerStream<SearchGoogleAdsStreamResponse> stream = googleAdsServiceClient.searchStreamCallable().call(request); // Iterates through and prints all of the results in the stream response. for (SearchGoogleAdsStreamResponse response : stream) { for (GoogleAdsRow googleAdsRow : response.getResultsList()) { System.out.printf( "Campaign with ID %d and name '%s' was found.%n", googleAdsRow.getCampaign().getId(), googleAdsRow.getCampaign().getName()); } } } }
C#
public void Run(GoogleAdsClient client, long customerId) { // Get the GoogleAdsService. GoogleAdsServiceClient googleAdsService = client.GetService( Services.V22.GoogleAdsService); // Create a query that will retrieve all campaigns. string query = @"SELECT campaign.id, campaign.name, campaign.network_settings.target_content_network FROM campaign ORDER BY campaign.id"; try { // Issue a search request. googleAdsService.SearchStream(customerId.ToString(), query, delegate (SearchGoogleAdsStreamResponse resp) { foreach (GoogleAdsRow googleAdsRow in resp.Results) { Console.WriteLine("Campaign with ID {0} and name '{1}' was found.", googleAdsRow.Campaign.Id, googleAdsRow.Campaign.Name); } } ); } catch (GoogleAdsException e) { Console.WriteLine("Failure:"); Console.WriteLine($"Message: {e.Message}"); Console.WriteLine($"Failure: {e.Failure}"); Console.WriteLine($"Request ID: {e.RequestId}"); throw; } }
PHP
public static function runExample(GoogleAdsClient $googleAdsClient, int $customerId) { $googleAdsServiceClient = $googleAdsClient->getGoogleAdsServiceClient(); // Creates a query that retrieves all campaigns. $query = 'SELECT campaign.id, campaign.name FROM campaign ORDER BY campaign.id'; // Issues a search stream request. /** @var GoogleAdsServerStreamDecorator $stream */ $stream = $googleAdsServiceClient->searchStream( SearchGoogleAdsStreamRequest::build($customerId, $query) ); // Iterates over all rows in all messages and prints the requested field values for // the campaign in each row. foreach ($stream->iterateAllElements() as $googleAdsRow) { /** @var GoogleAdsRow $googleAdsRow */ printf( "Campaign with ID %d and name '%s' was found.%s", $googleAdsRow->getCampaign()->getId(), $googleAdsRow->getCampaign()->getName(), PHP_EOL ); } }
Python
def main(client: GoogleAdsClient, customer_id: str) -> None: ga_service: GoogleAdsServiceClient = client.get_service("GoogleAdsService") query: str = """ SELECT campaign.id, campaign.name FROM campaign ORDER BY campaign.id""" # Issues a search request using streaming. stream: Iterator[SearchGoogleAdsStreamResponse] = ga_service.search_stream( customer_id=customer_id, query=query ) for batch in stream: rows: List[GoogleAdsRow] = batch.results for row in rows: print( f"Campaign with ID {row.campaign.id} and name " f'"{row.campaign.name}" was found.' )
Ruby
def get_campaigns(customer_id) # GoogleAdsClient will read a config file from # ENV['HOME']/google_ads_config.rb when called without parameters client = Google::Ads::GoogleAds::GoogleAdsClient.new responses = client.service.google_ads.search_stream( customer_id: customer_id, query: 'SELECT campaign.id, campaign.name FROM campaign ORDER BY campaign.id', ) responses.each do |response| response.results.each do |row| puts "Campaign with ID #{row.campaign.id} and name '#{row.campaign.name}' was found." end end end
Perl
sub get_campaigns { my ($api_client, $customer_id) = @_; # Create a search Google Ads stream request that will retrieve all campaigns. my $search_stream_request = Google::Ads::GoogleAds::V22::Services::GoogleAdsService::SearchGoogleAdsStreamRequest ->new({ customerId => $customer_id, query => "SELECT campaign.id, campaign.name FROM campaign ORDER BY campaign.id" }); # Get the GoogleAdsService. my $google_ads_service = $api_client->GoogleAdsService(); my $search_stream_handler = Google::Ads::GoogleAds::Utils::SearchStreamHandler->new({ service => $google_ads_service, request => $search_stream_request }); # Issue a search request and process the stream response to print the requested # field values for the campaign in each row. $search_stream_handler->process_contents( sub { my $google_ads_row = shift; printf "Campaign with ID %d and name '%s' was found.\n", $google_ads_row->{campaign}{id}, $google_ads_row->{campaign}{name}; }); return 1; }
Przykłady kodu
Zapoznaj się z przykładami kodu niektórych typowych funkcji w interfejsie Google Ads API.