Google Ads API Perl 用戶端程式庫提供多項設定,可供您自訂程式庫行為。
設定檔
您可以在例項化用戶端時,指定要使用的 googleads.properties 檔案。
如果您在例項化時未使用任何引數:
my $api_client = Google::Ads::GoogleAds::Client->new();
然後程式庫會在 HOME 目錄中尋找檔案,或在 GOOGLE_ADS_CONFIGURATION_FILE_PATH 環境變數中指定的路徑 (如有設定) 尋找檔案。
或者,您也可以明確指定路徑:
my $properties_file = "/path/to/googleads.properties";
my $api_client = Google::Ads::GoogleAds::Client->new({
properties_file => $properties_file,
});
在這種情況下,用戶端會尋找該路徑的檔案。
如要產生這個檔案,最簡單的方法是從 GitHub 存放區複製 googleads.properties,然後修改檔案,加入用戶端 ID、用戶端密鑰和更新權杖。
動態設定
您可以在例項化程式庫時動態設定設定,也可以在例項化後設定:
my $api_client = Google::Ads::GoogleAds::Client->new({
login_customer_id => "INSERT_LOGIN_CUSTOMER_ID_HERE",
});
您也可以在例項化後修改設定:
$api_client->set_login_customer_id("INSERT_LOGIN_CUSTOMER_ID_HERE");
您也可以從 Client 執行個體取得 OAuth2ApplicationsHandler 物件,並在執行階段變更用戶端 ID、用戶端密鑰和更新權杖:
my $oauth2_applications_handler =
$api_client->get_oauth2_applications_handler();
$oauth2_applications_handler->set_client_id("INSERT_CLIENT_ID_HERE");
$oauth2_applications_handler->set_client_secret("INSERT_CLIENT_SECRET_HERE");
$oauth2_applications_handler->set_refresh_token("INSERT_REFRESH_TOKEN_HERE");
設定環境變數
您可以在例項化用戶端時,透過環境變數設定部分設定 (請參閱標準環境變數)。
Client 模組提供 configure_from_environment_variables 函式,可從環境變數載入值。
# Get the Google Ads API Client. By default, any credentials are read from
# ~/googleads.properties, or, if set, from the file specified in the
# GOOGLE_ADS_CONFIGURATION_FILE_PATH environment variable.
my $api_client = Google::Ads::GoogleAds::Client->new();
# Load the configuration from any set environment variables.
$api_client->configure_from_environment_variables();
設定欄位
請注意,googleads.properties 鍵會使用 camelCase (例如 loginCustomerId 和 linkedCustomerId),而 Client->new({...}) 建構函式引數和處理常式方法則會使用 snake_case (例如 login_customer_id 和 linked_customer_id)。
OAuth2ApplicationsHandler 中保留的欄位:
client_id(googleads.properties中的clientId,GOOGLE_ADS_CLIENT_ID): 您的 OAuth2 用戶端 ID。client_secret(clientSecretingoogleads.properties,GOOGLE_ADS_CLIENT_SECRET):您的 OAuth2 用戶端密鑰。refresh_token(refreshTokeningoogleads.properties,GOOGLE_ADS_REFRESH_TOKEN):您的 OAuth2 更新權杖。additional_scopes(additionalScopesingoogleads.properties): 要要求的其他 OAuth2 範圍。
OAuth2ServiceAccountsHandler 中保留的欄位:
json_key_file_path(googleads.properties中的GOOGLE_ADS_JSON_KEY_FILE_PATHjsonKeyFilePath):服務帳戶 JSON 金鑰檔案的路徑。impersonated_email(impersonatedEmailingoogleads.properties,GOOGLE_ADS_IMPERSONATED_EMAIL):使用 Google Workspace 網域範圍授權時要模擬的電子郵件地址。additional_scopes(additionalScopesingoogleads.properties): 要要求的其他 OAuth2 範圍。
Client 中保留的欄位:
developer_token(developerTokeningoogleads.properties,GOOGLE_ADS_DEVELOPER_TOKEN):(2026 年 9 月 9 日終止服務) 您的 Google Ads API 開發人員權杖。 選用,無論用戶端程式庫版本為何,API 伺服器都會忽略此欄位,且 Google Ads API 未來的主要版本會拒絕此欄位。v35.0.0以上版本:不需要在用戶端初始化時提供 (v35.0.0已移除本機用戶端developerToken驗證)。v35.0.0之前的版本:如果您尚未升級至v35.0.0或更新版本,就必須通過本機用戶端設定驗證。
login_customer_id(googleads.properties中的loginCustomerId,GOOGLE_ADS_LOGIN_CUSTOMER_ID):用來存取客戶帳戶的管理員帳戶 ID。請參閱登入客戶 ID 說明文件。linked_customer_id(linkedCustomerIdingoogleads.properties,GOOGLE_ADS_LINKED_CUSTOMER_ID):已連結的客戶 ID。service_address(serviceAddressingoogleads.properties,GOOGLE_ADS_ENDPOINT):Google Ads API 服務位址網址 (預設為"https://googleads.googleapis.com")。user_agent(userAgentingoogleads.properties,GOOGLE_ADS_PERL_USER_AGENT):HTTP 請求中包含的自訂 User-Agent 標頭前置字元。proxy(proxyingoogleads.properties,GOOGLE_ADS_PERL_PROXY):用於網際網路連線的 Proxy 伺服器網址。version(Client->new或set_version):要使用的 Google Ads API 版本模組 (預設為"V25")。die_on_faults(Client->new或set_die_on_faults):設為1,讓服務方法在發生 API 錯誤時,呼叫die()並傳回原始回應酬載,而不是傳回Google::Ads::GoogleAds::GoogleAdsException物件 (預設為0)。http_timeout(Client->new或set_http_timeout):HTTP 逾時時間 (以秒為單位),預設為3600。http_retry_timing(Client->new或set_http_retry_timing):暫停重試間隔 (以秒為單位),適用於暫時性 HTTP503和504錯誤 (預設為"5,10,15")。