配置

该库会在 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

# 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

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

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

组合配置方法

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

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

在此示例中,客户端库将使用属性文件中的值 通过环境变量和 创建相应的条目对于相反的行为,只需调用 fromPropertiesFile()fromEnvironment()之前。

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