Cấu hình

Thư viện sẽ tìm tệp cấu hình trong System.getProperty("user.home") + "/ads.properties". Bạn có thể ghi đè đường dẫn và tên tệp này trong thời gian chạy khi tạo GoogleAdsClient bằng một trong các cơ chế sau:

  • Gọi fromPropertiesFile(PATH_TO_CONFIG_FILE), trong đó PATH_TO_CONFIG_FILE là đường dẫn và tên tệp của tệp cấu hình.
  • Đặt biến môi trường GOOGLE_ADS_CONFIGURATION_FILE_PATH thành đường dẫn và tên tệp của tệp cấu hình, sau đó gọi fromPropertiesFile().

Định dạng của tệp cấu hình là định dạng của tệp Thuộc tính Java của các cặp khoá-giá trị. Các khoá được hỗ trợ sẽ khác nhau, tuỳ thuộc vào quy trình xác thực mà bạn chọn.

Các khoá được hỗ trợ cho luồng ứng dụng web và máy tính

Nếu bạn đang sử dụng quy trình của ứng dụng trên máy tính hoặc web, thì các khoá được hỗ trợ sẽ như sau:

# 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

Khoá được hỗ trợ cho tài khoản dịch vụ

Nếu bạn đang sử dụng quy trình tài khoản dịch vụ, thì các khoá được hỗ trợ sẽ như sau:

# 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

Sử dụng các biến môi trường

Thư viện này hỗ trợ tất cả biến môi trường chung cho tất cả các thư viện ứng dụng API Google Ads. Bảng dưới đây cho thấy biến môi trường tương ứng với mỗi thuộc tính của tệp cấu hình.

Thuộc tính của tệp cấu hình Biến môi trường
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

Sau khi bạn đặt các biến môi trường thích hợp, hãy định cấu hình GoogleAdsClient bằng cách gọi fromEnvironment() trên trình tạo.

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

Kết hợp các phương pháp định cấu hình

GoogleAdsClient và tính năng hỗ trợ trình tạo kết hợp nhiều chiến lược cấu hình. Ví dụ: bạn có thể sử dụng các biến môi trường để định cấu hình thông tin xác thực của thực thể và tệp thuộc tính cho các thuộc tính khác bằng đoạn mã sau.

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

Trong ví dụ này, thư viện ứng dụng sẽ sử dụng giá trị từ tệp thuộc tính cho bất kỳ thuộc tính nào được xác định bằng cả biến môi trường và một mục nhập trong tệp thuộc tính. Đối với hành vi ngược lại, bạn chỉ cần gọi fromPropertiesFile() trước fromEnvironment().

Bạn có thể thực hiện các thay đổi khác trong thời gian chạy bằng cách sử dụng các phương thức cấu hình khác của trình tạo trước khi gọi build().