مكتبات العملاء

توفّر مكتبات برامجنا طرق عرض عالية المستوى والوحدات الأساسية لوظائف Google Ads API، ما يسهّل تطوير التطبيقات بسرعة. ننصحك بالبدء بواحدة إذا كنت جديدًا على واجهة برمجة التطبيقات.

مكتبة العميل المصدر التوزيع أمثلة للتعليمات البرمجية
Java google-ads-java Maven وtar.gz عرض على GitHub
‎.NET google-ads-dotnet nuget وtar.gz وzip عرض على GitHub
PHP google-ads-php tar.gz عرض على GitHub
Python google-ads-python ‫tar.gz وzip عرض على GitHub
Ruby google-ads-ruby ‫gem وtar.gz وzip عرض على GitHub
Perl google-ads-perl ‫tar.gz وzip عرض على GitHub

مكتبات المنتدى

نحن على عِلم بعدد من المكتبات التي يديرها منتدى البرامج المفتوحة المصدر. ونحتفظ بهذه القائمة لمساعدة المطوّرين المهتمّين في العثور عليها. لا نختبر هذه المكتبات أو نساهم فيها أو نديرها، لذا عليك استخدامها على مسؤوليتك الخاصة. يُرجى توجيه أي أسئلة إلى مالكي مشاريع البرامج المفتوحة المصدر.

إذا كنت على عِلم بمكتبة لم ندرجها، يُرجى النقر على إرسال ملاحظات في أعلى الصفحة وإعلامنا بها.

إصدارات واجهة برمجة التطبيقات المتوافقة

يعرض الجدول مكتبات برامج العميل التي تعمل مع إصدارات واجهة برمجة التطبيقات.

إنّ الحد الأدنى لإصدار مكتبة برامج العميل المدرَج كافٍ لإصدار Google Ads API الرئيسي (مثل الإصدار 24). للاطّلاع على إصدار مكتبة برامج العميل المطلوب للإصدارات الثانوية، يُرجى الرجوع إلى "سجلّ التغييرات" المرتبط.

Java

سجلّ التغييرات

مكتبة برامج Java
v24 Min: 43.0.0
Max: -
v23 Min: 42.0.0
Max: -
v22 Min: 41.0.0
Max: -
v21 Min: 39.0.0
Max: -

‫#C

سجلّ التغييرات

مكتبة برامج ‎NET.
v24 Min: 25.3.0
Max: -
v23 Min: 25.1.0
Max: -
v22 Min: 24.1.0
Max: -
v21 Min: 24.0.0
Max: -

PHP

سجلّ التغييرات

مكتبة برامج PHP
v24 Min: 33.3.0
Max: -
v23 Min: 32.2.0
Max: -
v22 Min: 31.0.0
Max: -
v21 Min: 28.0.0
Max: -

Python

سجلّ التغييرات

مكتبة برامج Python
v24 Min: 30.1.0
Max: -
v23 Min: 29.2.0
Max: -
v22 Min: 28.1.0
Max: -
v21 Min: 28.0.0
Max: -

Ruby

سجلّ التغييرات

مكتبة برامج Ruby
v24 Min: 40.0.0
Max: -
v23 Min: 38.0.0
Max: -
v22 Min: 36.0.0
Max: -
v21 Min: 35.0.0
Max: -

Perl

سجلّ التغييرات

مكتبة برامج Perl
v24 Min: 32.0.0
Max: -
v23 Min: 31.0.0
Max: -
v22 Min: 29.0.0
Max: -
v21 Min: 28.0.0
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" (مورد 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.V24.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::V24::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;
}
      

curl

أمثلة للتعليمات البرمجية

يمكنك الاطّلاع على أمثلة التعليمات البرمجية لبعض الوظائف الشائعة في Google Ads API.