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(googleads.properties中的clientSecret,GOOGLE_ADS_CLIENT_SECRET):您的 OAuth2 用戶端密鑰。refresh_token(googleads.properties中的refreshToken,GOOGLE_ADS_REFRESH_TOKEN):您的 OAuth2 更新權杖。additional_scopes(googleads.properties中的additionalScopes): 要要求的其他 OAuth2 範圍。
OAuth2ServiceAccountsHandler 中保留的欄位:
json_key_file_path(googleads.properties中的jsonKeyFilePath,GOOGLE_ADS_JSON_KEY_FILE_PATH):服務帳戶 JSON 金鑰檔案的路徑。impersonated_email(googleads.properties中的impersonatedEmail,GOOGLE_ADS_IMPERSONATED_EMAIL):使用 Google Workspace 全網域委派功能時要模擬的電子郵件地址。additional_scopes(googleads.properties中的additionalScopes): 要要求的其他 OAuth2 範圍。
Client 中保留的欄位:
login_customer_id(googleads.properties中的loginCustomerId,GOOGLE_ADS_LOGIN_CUSTOMER_ID):用於存取客戶帳戶的管理員帳戶 ID。請參閱 login-customer-id 說明文件。linked_customer_id(googleads.properties中的linkedCustomerId,GOOGLE_ADS_LINKED_CUSTOMER_ID):已連結的客戶 ID。service_address(googleads.properties中的serviceAddress,GOOGLE_ADS_ENDPOINT):Google Ads API 服務位址網址 (預設為"https://googleads.googleapis.com")。user_agent(userAgent中的googleads.properties,GOOGLE_ADS_PERL_USER_AGENT): HTTP 要求中包含的自訂使用者代理程式標頭前置字串。proxy(googleads.properties中的proxy,GOOGLE_ADS_PERL_PROXY):用於網際網路連線的 Proxy 伺服器網址。version(Client->new或set_version):要使用的 Google Ads API 版本模組 (預設為"V26")。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")。