設定

Java 用戶端程式庫會在主目錄 (System.getProperty("user.home") + "/ads.properties" 或 ~/ads.properties) 中尋找名為 ads.properties 的設定檔。您可以在建構 GoogleAdsClient 時,使用下列任一機制在執行階段覆寫這個路徑和檔案名稱:

  • 呼叫 fromPropertiesFile(PATH_TO_CONFIG_FILE),其中 PATH_TO_CONFIG_FILE 是設定檔的 File 路徑和檔案名稱。
  • 將環境變數 GOOGLE_ADS_CONFIGURATION_FILE_PATH 設為設定檔的路徑和檔案名稱,然後呼叫 fromPropertiesFile()。

設定檔的格式為標準 Java Properties 檔案,內含鍵/值組合。支援的金鑰會因所選驗證流程而異。

支援電腦和網頁應用程式流程的金鑰

如果您使用單一使用者或多使用者應用程式流程,支援的金鑰如下:

# 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

# 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

# Maximum allowed response payload size, in bytes.
# Customize this to allow response sizes for GoogleAdsService.search and
# GoogleAdsService.searchStream API calls to exceed the default limit of 64MB.
# api.googleads.maxInboundMessageBytes=INSERT_MAX_INBOUND_MESSAGE_BYTES_HERE

# Specifies whether to use Application Default Credentials.
api.googleads.useApplicationDefaultCredentials=false

服務帳戶支援的金鑰

如果您使用服務帳戶流程,支援的金鑰如下:

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

# Optional: Email address of the user to impersonate when using Google Workspace
# domain-wide delegation. This should be a user who has access to your Google Ads
# account and is in the same Google Workspace domain as the service account.
# api.googleads.serviceAccountUser=INSERT_USER_EMAIL_ADDRESS_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.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
api.googleads.maxInboundMessageBytes GOOGLE_ADS_MAX_INBOUND_MESSAGE_BYTES
api.googleads.useApplicationDefaultCredentials GOOGLE_ADS_USE_APPLICATION_DEFAULT_CREDENTIALS
api.googleads.developerToken (在 v46.0.0 以上版本中為選用) GOOGLE_ADS_DEVELOPER_TOKEN

設定適當的環境變數後,請在建構工具上呼叫 fromEnvironment(),設定 GoogleAdsClient:

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

結合設定方法

GoogleAdsClient 和其建構工具支援合併不同的設定策略。舉例來說,您可以使用環境變數設定例項的憑證,以及其他屬性的屬性檔案,程式碼片段如下:

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

呼叫 build() 之前,您可以使用建構工具的其他設定方法,在執行階段進行進一步變更。

開發人員權杖停用

2026 年 9 月 9 日開發人員權杖停用後,API 存取層級將由 API 伺服器上的 Google Cloud 雲端專案決定,而非開發人員權杖:

  • 設定變更:您不再需要在環境變數中指定 api.googleads.developerToken ads.properties 或 GOOGLE_ADS_DEVELOPER_TOKEN。現有應用程式仍指定 api.googleads.developerToken,因此會繼續運作,因為 API 伺服器會忽略 developer-token 標頭 (但 Google Ads API 未來的主要版本會拒絕)。
  • 用戶端程式庫版本規定:如要從設定中省略開發人員權杖,請使用 google-ads-java v46.0.0 以上版本,該版本已移除需要開發人員權杖的用戶端驗證。
  • 授權錯誤變更:如果只有測試存取權的 Google Cloud 專案呼叫正式版 Google Ads 帳戶,則 Google Ads API v25 以上版本會傳回 AuthorizationError.CLOUD_PROJECT_NOT_APPROVED_FOR_PRODUCTION,而 v24 以下版本則會傳回 AuthorizationError.ACTION_NOT_PERMITTED。