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

توفّر مكتبات العملاء طرق عرض عالية المستوى والوحدات الأساسية لوظائف 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 الرئيسي (مثل الإصدار 25). للاطّلاع على إصدار مكتبة البرامج المطلوب للإصدارات الثانوية، يُرجى الرجوع إلى سجلّ التغيير المرتبط.

جافا

سجلّ التغيير

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

#C

سجلّ التغيير

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

PHP

سجلّ التغيير

مكتبة برامج للغة PHP
v25 Min: 33.6.0
Max: -
v24 Min: 33.3.0
Max: -
v23 Min: 32.2.0
Max: -
v22 Min: 31.0.0
Max: -

Python

سجلّ التغيير

مكتبة برامج Python
v25 Min: 31.2.0
Max: -
v24 Min: 30.1.0
Max: -
v23 Min: 29.2.0
Max: -
v22 Min: 28.1.0
Max: 32.0.0

Ruby

سجلّ التغيير

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

Perl

سجلّ التغيير

مكتبة برامج Perl
v25 Min: 33.0.0
Max: -
v24 Min: 32.0.0
Max: -
v23 Min: 31.0.0
Max: -
v22 Min: 29.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_LOGIN_CUSTOMER_ID : هذا هو رقم تعريف العميل المصرّح له باستخدامه في الطلب، بدون شُرط (-).
    • GOOGLE_ADS_LINKED_CUSTOMER_ID : هذا العنوان مطلوب فقط للطُرق التي تعدّل موارد كيان عندما يتم منح الإذن من خلال "الحسابات المرتبطة" في واجهة مستخدم "إعلانات Google" (المورد AccountLink في Google Ads API). اضبط هذه القيمة على رقم تعريف العميل الخاص بموفّر البيانات الذي يعدّل موارد رقم تعريف العميل المحدّد. يجب ضبطه بدون واصلات (-). لمزيد من المعلومات حول "الحسابات المرتبطة"، يُرجى الانتقال إلى مركز المساعدة.

يتم عادةً تحديد متغيّرات البيئة في ملف إعداد باش، مثل ملف .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 صفحة لكل صفحة.

تتولّى مكتبة برامجنا تلقائيًا تنفيذ التقسيم إلى صفحات عند تكرار النتائج، ما يتيح لك تنزيلها ومعالجتها بالتسلسل دفعة واحدة.

جافا

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.V25.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::V25::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.