配置

该库会在 System.getProperty("user.home") + "/ads.properties" 中查找配置文件。使用以下任一机制构建 GoogleAdsClient 时,您可以在运行时替换此路径和文件名:

  • 调用 fromPropertiesFile(PATH_TO_CONFIG_FILE),其中 PATH_TO_CONFIG_FILE 是配置文件的路径和文件名。
  • 将环境变量 GOOGLE_ADS_CONFIGURATION_FILE_PATH 设置为配置文件的路径和文件名,然后调用 fromPropertiesFile()

配置文件格式为键值对的 Java Properties 文件。支持的密钥因所选的身份验证流程而异。

适用于桌面和 Web 应用流程的受支持密钥

如果您使用的是桌面应用流程或网页应用流程,则支持的键如下:

# 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

设置适当的环境变量后,通过对构建器调用 fromEnvironment() 来配置 GoogleAdsClient

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

组合使用配置方法

GoogleAdsClient 及其构建器支持组合不同的配置策略。例如,借助以下代码段,您可以使用环境变量来配置实例的凭据和其他属性的属性文件。

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

在此示例中,客户端库将使用属性文件中的值,用于同时通过其环境变量和属性文件中的某个条目定义的任何属性。如需获得相反的行为,只需在 fromEnvironment() 之前调用 fromPropertiesFile() 即可。

您可以在调用 build() 之前在运行时使用构建器的其他配置方法进行进一步更改。