程式庫會在 System.getProperty("user.home") + "/ads.properties"
中尋找設定檔。您可以在使用下列任一機制建構 GoogleAdsClient
時,在執行階段覆寫這個路徑和檔案名稱:
- 請呼叫
fromPropertiesFile(PATH_TO_CONFIG_FILE)
,其中PATH_TO_CONFIG_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
# 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
# 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
服務帳戶支援的金鑰
如果您使用服務帳戶流程,支援的金鑰如下:
# 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 |
api.googleads.maxInboundMessageBytes |
GOOGLE_ADS_MAX_INBOUND_MESSAGE_BYTES |
設定適當的環境變數後,請在建構工具上呼叫 fromEnvironment()
,藉此設定 GoogleAdsClient
。
GoogleAdsClient googleAdsClient = GoogleAdsClient.newBuilder()
.fromEnvironment()
.build();
結合設定方法
GoogleAdsClient
及其建構工具支援結合不同的設定策略。舉例來說,您可以使用環境變數設定執行個體的憑證,並使用下列程式碼片段設定其他屬性的屬性檔案。
GoogleAdsClient googleAdsClient = GoogleAdsClient.newBuilder()
.fromEnvironment()
.fromPropertiesFile()
.build();
在這個範例中,如果屬性是透過環境變數和屬性檔案中的項目同時定義,用戶端程式庫就會使用屬性檔案中的值。如要執行相反的行為,只要在 fromEnvironment()
之前呼叫 fromPropertiesFile()
即可。
您可以在呼叫 build()
之前,使用建構工具的其他設定方法在執行階段進行進一步變更。