Thư viện ứng dụng của chúng tôi cung cấp các chế độ xem cấp cao và các khối xây dựng cơ bản của chức năng Google Ads API, giúp bạn dễ dàng phát triển ứng dụng một cách nhanh chóng. Bạn nên bắt đầu với một API nếu mới làm quen với API này.
Thư viện ứng dụng | Nguồn | Hoạt động phân phối | Ví dụ về mã |
---|---|---|---|
Java | google-ads-java | Maven, tar.gz | Xem trên GitHub |
.NET | google-ads-dotnet | nuget, tar.gz, zip | Xem trên GitHub |
PHP | google-ads-php | tar.gz | Xem trên GitHub |
Python | google-ads-python | tar.gz, zip | Xem trên GitHub |
Ruby | google-ads-ruby | gem, tar.gz, zip | Xem trên GitHub |
Perl | google-ads-perl | tar.gz, zip | Xem trên GitHub |
Thư viện cộng đồng
Chúng tôi biết đến một số thư viện do cộng đồng nguồn mở duy trì. Chúng tôi duy trì danh sách này để giúp các nhà phát triển quan tâm tìm thấy những nhà phát triển khác. Chúng tôi không kiểm thử, đóng góp hoặc duy trì các thư viện này; hãy sử dụng chúng theo ý bạn. Bạn nên gửi mọi câu hỏi cho chủ sở hữu của các dự án nguồn mở.
Nếu bạn biết có thư viện mà chúng tôi chưa đề cập, hãy nhấp vào Gửi ý kiến phản hồi ở đầu trang và cho chúng tôi biết.
Truy cập vào: https://github.com/shenzhencenter/google-ads-pb
Các phiên bản API được hỗ trợ
Bảng này cho biết những thư viện ứng dụng nào hoạt động với những phiên bản API nào.
Java
API Google Ads |
Thư viện ứng dụng cho Java |
v20 |
Min: 38.0.0 Max: – |
v19 |
Min: 36.0.0 Max: – |
v18 |
Min: 34.0.0 Max: – |
v17 |
Min: 31.0.0 Max: 36.1.0 |
C#
API Google Ads |
Thư viện ứng dụng cho .NET |
v20 |
Min: 23.0.0 Max: – |
v19 |
Min: 22.1.0 Max: – |
v18 |
Min: 21.1.0 Max: – |
v17 |
Min: 20.1.0 Max: 22.2.0 |
PHP
API Google Ads |
Thư viện ứng dụng cho PHP |
v20 |
Min: 28.0.0 Max: – |
v19 |
Min: 26.1.0 Max: – |
v18 |
Min: 25.0.0 Max: – |
v17 |
Min: 23.1.0 Max: 27.2.0
|
Python
API Google Ads |
Thư viện ứng dụng cho Python |
v20 |
Min: 27.0.0 Max: – |
v19 |
Min: 25.2.0 Max: – |
v18 |
Min: 25.1.0 Max: – |
v17 |
Min: 24.1.0 Max: 26.1.0 |
Ruby
API Google Ads |
Thư viện ứng dụng cho Ruby |
v20 |
Min: 34.0.0 Max: – |
v19 |
Min: 32.0.0 Max: – |
v18 |
Min: 31.0.0 Max: – |
Perl
API Google Ads |
Thư viện ứng dụng cho Perl |
v20 |
Min: 27.0.0 Max: – |
v19 |
Min: 26.0.0 Max: – |
v18 |
Min: 25.0.1 Max: – |
Cấu hình
Mỗi thư viện ứng dụng Ads API đều cung cấp các chế độ cài đặt cấu hình và phương thức tải khác nhau mà bạn có thể dùng để tuỳ chỉnh hành vi của thư viện.
Dưới đây là các biến môi trường thường gặp trong tất cả các thư viện ứng dụng và có thể được tải để thiết lập chế độ cài đặt cấu hình:
- Thư viện ứng dụng
GOOGLE_ADS_CONFIGURATION_FILE_PATH
: Đường dẫn đến tệp cấu hình.
- OAuth2
- Chế độ ứng dụng
GOOGLE_ADS_CLIENT_ID
: Đặt giá trị này thành mã ứng dụng OAuth2 của bạn.GOOGLE_ADS_CLIENT_SECRET
: Đặt giá trị này thành khoá bí mật của ứng dụng OAuth2.GOOGLE_ADS_REFRESH_TOKEN
: Đặt giá trị này thành mã làm mới OAuth2 được tạo trước nếu bạn muốn sử dụng lại mã thông báo OAuth2. Bạn không bắt buộc phải sử dụng chế độ cài đặt này.
- Chế độ tài khoản dịch vụ
GOOGLE_ADS_JSON_KEY_FILE_PATH
: Đặt giá trị này thành đường dẫn đến tệp cấu hình JSON OAuth2.GOOGLE_ADS_IMPERSONATED_EMAIL
: Đặt giá trị này thành địa chỉ email của tài khoản mà bạn đang mạo danh.
- Chế độ ứng dụng
- Google Ads API
GOOGLE_ADS_DEVELOPER_TOKEN
: Đặt mã này thành mã thông báo của nhà phát triển.GOOGLE_ADS_LOGIN_CUSTOMER_ID
: Đây là mã khách hàng của khách hàng được uỷ quyền sử dụng trong yêu cầu, không có dấu gạch nối (-
).GOOGLE_ADS_LINKED_CUSTOMER_ID
: Tiêu đề này chỉ bắt buộc đối với những phương thức cập nhật tài nguyên của một thực thể khi được cấp quyền thông qua Tài khoản được liên kết trong giao diện người dùng Google Ads (tài nguyênAccountLink
trong Google Ads API). Đặt giá trị này thành mã khách hàng của nhà cung cấp dữ liệu cập nhật tài nguyên của mã khách hàng được chỉ định. Bạn nên đặt mã này mà không có dấu gạch ngang (-
). Để tìm hiểu thêm về Tài khoản được liên kết, hãy truy cập Trung tâm trợ giúp.
Các biến môi trường thường được xác định trong tệp cấu hình bash, chẳng hạn như tệp .bashrc
hoặc .bash_profile
nằm trong thư mục $HOME
. Bạn cũng có thể xác định các tham số này bằng dòng lệnh.
Dưới đây là một số bước cơ bản để xác định một biến môi trường bằng tệp .bashrc
thông qua một thiết bị đầu cuối:
# 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
Bạn cũng có thể thiết lập các biến môi trường trong phiên bản thiết bị đầu cuối ngay từ dòng lệnh:
export GOOGLE_ADS_CLIENT_ID=1234567890
Một lựa chọn khác là đặt các biến môi trường khi gọi lệnh sử dụng các biến đó:
GOOGLE_ADS_CLIENT_ID=1234567890 php /path/to/script/that/uses/envvar.php
Tìm nạp thực thể
GoogleAdsService.SearchStream
thường được dùng để tìm nạp các thực thể và kết quả được trả về dưới dạng một luồng hàng. Ngoài ra, bạn có thể dùng GoogleAdsService.Search
để tìm nạp các thực thể qua một kết nối Internet không ổn định. GoogleAdsService.Search
trả về kết quả dưới dạng kích thước trang cố định là 10.000 hàng trên mỗi trang.
Thư viện máy khách của chúng tôi sẽ tự động triển khai tính năng phân trang khi bạn lặp lại kết quả để có thể tải xuống và xử lý tất cả cùng một lúc.
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.V20.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, customer_id): ga_service = client.get_service("GoogleAdsService") query = """ SELECT campaign.id, campaign.name FROM campaign ORDER BY campaign.id""" # Issues a search request using streaming. stream = ga_service.search_stream(customer_id=customer_id, query=query) for batch in stream: for row in batch.results: 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::V20::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; }
Ví dụ về mã
Xem các ví dụ về mã của một số hàm phổ biến trong API Google Ads.