该库会在 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()
之前使用构建器的其他配置方法在运行时进行进一步更改。