Java 客户端库会在您的主目录(System.getProperty("user.home") + "/ads.properties" 或 ~/ads.properties)中查找名为 ads.properties 的配置文件。您可以在运行时使用以下任一机制构造 GoogleAdsClient 时替换此路径和文件名:
- 调用
fromPropertiesFile(PATH_TO_CONFIG_FILE),其中PATH_TO_CONFIG_FILE是配置文件的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
# 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
# Specifies whether to use Application Default Credentials.
api.googleads.useApplicationDefaultCredentials=false
服务账号支持的密钥
如果您使用的是服务账号流程,则支持的密钥如下所示:
# Path to the service account secrets file in JSON format.
# Provided by console.cloud.google.com.
api.googleads.serviceAccountSecretsPath=INSERT_PATH_TO_JSON_HERE
# Optional: Email address of the user to impersonate when using Google Workspace
# domain-wide delegation. This should be a user who has access to your Google Ads
# account and is in the same Google Workspace domain as the service account.
# api.googleads.serviceAccountUser=INSERT_USER_EMAIL_ADDRESS_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.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 |
api.googleads.useApplicationDefaultCredentials |
GOOGLE_ADS_USE_APPLICATION_DEFAULT_CREDENTIALS |
api.googleads.developerToken(在 v46.0.0 及更高版本中为可选) |
GOOGLE_ADS_DEVELOPER_TOKEN |
设置好相应的环境变量后,通过在 build 上调用 fromEnvironment() 来配置 GoogleAdsClient:
GoogleAdsClient googleAdsClient =
GoogleAdsClient.newBuilder()
.fromEnvironment()
.build();
组合配置方法
GoogleAdsClient 及其构建器支持组合不同的配置策略。例如,您可以使用环境变量来配置实例的凭据,并使用以下代码段来配置其他属性的属性文件:
GoogleAdsClient googleAdsClient =
GoogleAdsClient.newBuilder()
.fromEnvironment()
.fromPropertiesFile()
.build();
在调用 build() 之前,您可以使用构建器的其他配置方法在运行时进行进一步更改。
开发者令牌即将停用
在 2026 年 9 月 9 日开发者令牌停用后,API 访问权限级别将由 API 服务器上的 Google Cloud 项目决定,而不是由开发者令牌决定:
- 配置更改:您不再需要在环境变量中指定
ads.properties中的api.googleads.developerToken或GOOGLE_ADS_DEVELOPER_TOKEN。仍指定api.googleads.developerToken的现有应用会继续正常运行,因为 API 服务器会忽略developer-token标头(不过,未来的 Google Ads API 主要版本会拒绝该标头)。 - 客户端库版本要求:如需从配置中省略开发者令牌,请使用
google-ads-javav46.0.0或更高版本,该版本移除了需要开发者令牌的客户端验证。 - 授权错误变更:如果仅具有测试访问权限的 Google Cloud 项目调用生产 Google Ads 账号,
v25及更高版本的 Google Ads API 会返回AuthorizationError.CLOUD_PROJECT_NOT_APPROVED_FOR_PRODUCTION,而v24及更低版本会返回AuthorizationError.ACTION_NOT_PERMITTED。