配置

使用集合让一切井井有条 根据您的偏好保存内容并对其进行分类。

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

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

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

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

如果您使用的是桌面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()

重复使用 Java 版 AdWords API 客户端库中的配置文件

您可以使用与 AdWords API 相同的 ads.properties 文件。只需复制以下键的行,并将每个键中的 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).