ספריות הלקוח שלנו מספקות תצוגות ברמה גבוהה ובלוקים בסיסיים של פונקציונליות Google Ads API, וכך מקלות על פיתוח אפליקציות במהירות. אם אתם חדשים ב-API, מומלץ להתחיל עם אחד.
ספריות קהילתיות
אנחנו מכירים כמה ספריות שמתוחזקות על ידי קהילת הקוד הפתוח. אנחנו מנהלים את הרשימה הזו כדי לעזור למפתחים שמתעניינים בכך למצוא אותם. אנחנו לא בודקים את הספריות האלה, לא תורמים להן ולא מתחזקים אותן. השימוש בהן הוא על אחריותכם בלבד. אם יש לכם שאלות, עליכם לפנות לבעלים של פרויקטים בקוד פתוח.
אם אתם מכירים ספרייה שלא ציינו כאן, אתם יכולים ללחוץ על שליחת משוב בחלק העליון של הדף ולעדכן אותנו.
גרסאות נתמכות של ה-API
בטבלה מוצגות ספריות הלקוח שפועלות עם גרסאות ה-API השונות.
Java
Google Ads API |
ספריית לקוח ל-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#
Google Ads API |
ספריית לקוח ל- .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
Google Ads API |
ספריית לקוח ל-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
Google Ads API |
ספריית לקוח ל-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
Google Ads API |
ספריית לקוח ל-Ruby |
v20 |
Min: 34.0.0 Max: - |
v19 |
Min: 32.0.0 Max: - |
v18 |
Min: 31.0.0 Max: - |
Perl
Google Ads API |
ספריית לקוח ל-Perl |
v20 |
Min: 27.0.0 Max: - |
v19 |
Min: 26.0.0 Max: - |
v18 |
Min: 25.0.1 Max: - |
תצורה
כל ספריית לקוח של Ads API מספקת הגדרות תצורה שונות ושיטות טעינה שבהן אפשר להשתמש כדי להתאים אישית את ההתנהגות שלה.
אלה משתני הסביבה שמשותפים לכל ספריות הלקוח, ואפשר לטעון אותם כדי להגדיר את הגדרות התצורה:
- ספריית לקוח
-
GOOGLE_ADS_CONFIGURATION_FILE_PATH
: הנתיב לקובץ התצורה.
-
- OAuth2
- מצב אפליקציה
-
GOOGLE_ADS_CLIENT_ID
: מגדירים את הערך הזה למזהה הלקוח ב-OAuth2. -
GOOGLE_ADS_CLIENT_SECRET
: מגדירים את הערך הזה לסוד הלקוח ב-OAuth2. -
GOOGLE_ADS_REFRESH_TOKEN
: אם רוצים לעשות שימוש חוזר בטוקנים של OAuth2, צריך להגדיר את הערך הזה לטוקן רענון של OAuth2 שנוצר מראש. ההגדרה הזו אופציונלית.
-
- מצב חשבון שירות
-
GOOGLE_ADS_JSON_KEY_FILE_PATH
: מגדירים את הערך הזה לנתיב של קובץ ה-JSON של הגדרות OAuth2. -
GOOGLE_ADS_IMPERSONATED_EMAIL
: מגדירים את הערך הזה לכתובת האימייל של החשבון שאתם מתחזים לו.
-
- מצב אפליקציה
- Google Ads API
-
GOOGLE_ADS_DEVELOPER_TOKEN
: מגדירים את הערך הזה לקוד המפתח. -
GOOGLE_ADS_LOGIN_CUSTOMER_ID
: מספר הלקוח המורשה לשימוש בבקשה, בלי מקפים (-
). -
GOOGLE_ADS_LINKED_CUSTOMER_ID
: הכותרת הזו נדרשת רק לשיטות שמעדכנות את המשאבים של ישות מסוימת כשההרשאה ניתנת דרך חשבונות מקושרים בממשק המשתמש של Google Ads (משאבAccountLink
ב-Google Ads API). מגדירים את הערך הזה למזהה הלקוח של ספק הנתונים שמעדכן את המשאבים של מזהה הלקוח שצוין. צריך להגדיר אותו בלי מקפים (-
). מידע נוסף על חשבונות מקושרים זמין במרכז העזרה.
-
בדרך כלל, משתני סביבה מוגדרים בקובץ תצורה של bash, כמו קובץ .bashrc
או .bash_profile
שנמצא בספרייה $HOME
. אפשר גם להגדיר אותם באמצעות שורת הפקודה.
כדי להגדיר משתנה סביבה באמצעות קובץ .bashrc
באמצעות מסוף, מבצעים את השלבים הבסיסיים הבאים:
# 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
אפשר להגדיר משתני סביבה גם ישירות במופע של הטרמינל משורת הפקודה:
export GOOGLE_ADS_CLIENT_ID=1234567890
אפשרות נוספת היא להגדיר משתני סביבה כשמפעילים את הפקודה שמשתמשת בהם:
GOOGLE_ADS_CLIENT_ID=1234567890 php /path/to/script/that/uses/envvar.php
אחזור ישויות
GoogleAdsService.SearchStream
משמש בדרך כלל לאחזור ישויות, והתוצאות מוחזרות כזרם של שורות. לחלופין, אפשר להשתמש ב-GoogleAdsService.Search
כדי לאחזר ישויות דרך חיבור אינטרנט לא אמין. GoogleAdsService.Search
מחזירה תוצאות בגודל דף קבוע של 10,000 שורות לכל דף.
ספריית הלקוח שלנו מיישמת באופן אוטומטי את האפשרות של חלוקה לדפים כשמבצעים איטרציה של התוצאות, כך שאפשר להוריד ולעבד את כולן בבת אחת.
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; }
דוגמאות לקוד
כאן אפשר לעיין בדוגמאות קוד של כמה פונקציות נפוצות ב-Google Ads API.