الإعداد

تبحث المكتبة عن ملف الإعداد في System.getProperty("user.home") + "/ads.properties". يمكنك إلغاء هذا المسار واسم الملف في وقت التشغيل عند إنشاء GoogleAdsClient باستخدام أي من الآليات التالية:

  • اطلب من fromPropertiesFile(PATH_TO_CONFIG_FILE)، حيث يكون PATH_TO_CONFIG_FILE المسار واسم ملف الإعداد.
  • اضبط متغيّر البيئة GOOGLE_ADS_CONFIGURATION_FILE_PATH على مسار الملف واسم الملف، ثم اطلب fromPropertiesFile().

ويكون تنسيق ملف الإعداد بتنسيق ملف الخصائص الخاص بـ Java لأزواج القيمة الأساسية. تختلف المفاتيح المتوافقة بناءً على مسار المصادقة الذي اخترته.

المفاتيح المتوافقة لتدفقات تطبيقات الويب وأجهزة الكمبيوتر المكتبي

إذا كنت تستخدم مسار التطبيق كمبيوتر مكتبي أو الويب، يمكنك تنفيذ ما يلي على المفاتيح التالية:

# Credential for accessing Google's OAuth servers.
# Provided by console.cloud.google.com.
api.googleads.clientId=INSERT_CLIENT_ID_HERE

# Credential for accessing Google's OAuth servers.
# Provided by console.cloud.google.com.
api.googleads.clientSecret=INSERT_CLIENT_SECRET_HERE

# Renewable OAuth credential associated with 1 or more Google Ads accounts.
api.googleads.refreshToken=INSERT_REFRESH_TOKEN_HERE

# Token which provides access to the Google Ads API in general. It does not
# grant access to any particular ad account (OAuth is used for this purpose).
api.googleads.developerToken=INSERT_DEVELOPER_TOKEN_HERE

# Required for manager accounts only: Specify the login customer ID used to
# authenticate API calls. This will be the customer ID of the authenticated
# manager account. You can also specify this later in code if your application
# uses multiple manager account + OAuth pairs.
#
# api.googleads.loginCustomerId=INSERT_LOGIN_CUSTOMER_ID_HERE

# Only required if explicitly instructed by the service documentation.
# api.googleads.linkedCustomerId=INSERT_LINKED_CUSTOMER_ID_HERE

المفاتيح المتوافقة لحسابات الخدمة

إذا كنت تستخدم مسار حساب الخدمة، يمكنك استخدام المفاتيح التالية:

# Path to the service account secrets file in JSON format.
# Provided by console.cloud.google.com.
api.googleads.serviceAccountSecretsPath=INSERT_PATH_TO_JSON_HERE

# Email address of the user to impersonate.
# This should be a user who has access to your Google Ads account and is in the same
# Google Apps Domain as the service account.
api.googleads.serviceAccountUser=INSERT_USER_EMAIL_ADDRESS_HERE

# Token which provides access to the Google Ads API in general. It does not
# grant access to any particular ad account (OAuth is used for this purpose).
api.googleads.developerToken=INSERT_DEVELOPER_TOKEN_HERE

# Required for manager accounts only: Specify the login customer ID used to
# authenticate API calls. This will be the customer ID of the authenticated
# manager account. You can also specify this later in code if your application
# uses multiple manager account + OAuth pairs.
#
# api.googleads.loginCustomerId=INSERT_LOGIN_CUSTOMER_ID_HERE

استخدام متغيّرات البيئة

تتوافق المكتبة مع جميع متغيّرات البيئة الشائعة في كل مكتبات برامج Google Ads API. يعرض الجدول أدناه متغيّر البيئة الذي يتوافق مع كل خاصية ملف إعداد.

موقع ملف الإعداد متغيّر البيئة
api.googleads.developerToken GOOGLE_ADS_DEVELOPER_TOKEN
api.googleads.clientId GOOGLE_ADS_CLIENT_ID
api.googleads.clientSecret GOOGLE_ADS_CLIENT_SECRET
api.googleads.refreshToken GOOGLE_ADS_REFRESH_TOKEN
api.googleads.serviceAccountSecretsPath GOOGLE_ADS_JSON_KEY_FILE_PATH
api.googleads.serviceAccountUser GOOGLE_ADS_IMPERSONATED_EMAIL
api.googleads.loginCustomerId GOOGLE_ADS_LOGIN_CUSTOMER_ID
api.googleads.linkedCustomerId GOOGLE_ADS_LINKED_CUSTOMER_ID

بعد ضبط متغيّرات البيئة المناسبة، يمكنك ضبط GoogleAdsClient من خلال طلب fromEnvironment() على أداة الإنشاء.

GoogleAdsClient googleAdsClient = GoogleAdsClient.newBuilder()
  .fromEnvironment()
  .build();

الجمع بين أساليب الإعداد

تدعم GoogleAdsClient وأداة الإنشاء هذه استراتيجيات ضبط مختلفة. على سبيل المثال، يمكنك استخدام متغيّرات البيئة لضبط بيانات اعتماد المثيل وملف خصائص للسمات الأخرى باستخدام المقتطف التالي.

GoogleAdsClient googleAdsClient = GoogleAdsClient.newBuilder()
    .fromEnvironment()
    .fromPropertiesFile()
    .build();

في هذا المثال، ستستخدم مكتبة البرامج القيمة من ملف الخصائص لأي سمة يتم تحديدها من خلال متغيّر البيئة أو إدخال في ملف الخصائص. بالنسبة إلى السلوك المعاكس، ما عليك سوى الاتصال fromPropertiesFile() قبل fromEnvironment().

ويمكنك إجراء المزيد من التغييرات في وقت التشغيل باستخدام طرق الإعداد الأخرى لأداة الإنشاء قبل استدعاء build().

إعادة استخدام ملفات الإعداد من مكتبة برامج AdWords API للغة Java

يمكنك استخدام ملف ads.properties نفسه الذي استخدمته لحساب AdWords API. ما عليك سوى نسخ أسطر المفاتيح التالية وتغيير بادئة api.adwords في كل مفتاح من api.adwords إلى api.googleads:

api.adwords.developerToken     --> api.googleads.developerToken
# For desktop or web OAuth credentials:
api.adwords.clientId           --> api.googleads.clientId
api.adwords.clientSecret       --> api.googleads.clientSecret
api.adwords.refreshToken       --> api.googleads.refreshToken
# For service account OAuth credentials:
api.adwords.jsonKeyFilePath    --> api.googleads.serviceAccountSecretsPath
api.adwords.serviceAccountUser --> api.googleads.serviceAccountUser

إذا كنت تُجري المصادقة كحساب إداري، عليك أيضًا تحديد ما يلي:

api.googleads.loginCustomerId --> Manager account ID (with hyphens removed).