設定
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
您可以使用 Config
類別進行設定。您可以在
例如編寫設定檔、在建立時指定值,或
從環境變數載入值。
設定檔
您可以指定
google_ads_config.rb
敬上
檔案。
如果您在例項化時不使用引數:
client = Google::Ads::GoogleAds::GoogleAdsClient.new
則程式庫會搜尋
GOOGLE_ADS_CONFIGURATION_FILE_PATH
環境變數。如果該變數是
如未設定,程式庫會在 HOME
目錄中尋找該檔案。
或者,您也可以指定路徑:
client = Google::Ads::GoogleAds::GoogleAdsClient.new("path/to/file.rb")
在此情況下,用戶端會在該檔案路徑中尋找檔案。
產生這個檔案最簡單的方式,是將 google_ads_config.rb
從
將其修改為加入更新權杖、用戶端 ID
和用戶端密碼
動態設定
您可以在建立程式庫例項時,以動態方式進行設定。
甚至之後:
client = Google::Ads::GoogleAds::GoogleAdsClient.new do |config|
config.client_id = 'INSERT_CLIENT_ID_HERE'
# ... more configuration
end
您甚至可以在建立例項後修改設定:
client.configure do |config|
config.login_customer_id = 'INSERT_LOGIN_CUSTOMER_ID_HERE'
# ... more configuration
end
設定欄位
Config
物件支援下列欄位。
一般欄位:
refresh_token
:您的 OAuth 更新憑證。
client_id
:您的 OAuth 用戶端 ID。
client_secret
:您的 OAuth 用戶端密鑰。
developer_token
:用來存取 API 的開發人員權杖。
login_customer_id
:請參閱
login-customer-id 說明文件。
記錄欄位。如需完整的詳細資訊,請參閱記錄指南。
log_level
:您要記錄的最低記錄層級訊息。例如:
指定 'DEBUG'
可確保查看所有記錄訊息
'INFO'
會排除 DEBUG 郵件,但顯示其他所有訊息。
log_target
:您想登入的位置,例如 STDERR
。
logger
:指定您自己的自訂記錄器。指定這個選項會覆寫
《log_level
》和《log_target
》。
環境變數
您可以使用環境變數來設定用戶端程式庫。他們不是
預設會載入,而且在實例化
用戶端載入版本。查看環境變數的完整清單
所有支援的
語言。
如要將環境變數載入 Config
,請呼叫 load_environment_config
:
client = Google::Ads::GoogleAds::GoogleAdsClient.new
client.load_environment_config
每次呼叫這個方法時,環境變數的目前狀態就會
覆寫 Config
中現有的任何內容。使用這項
機制,例如採用預設設定,但會覆寫特定
定義自己的環境變數,或者完全以
環境變數
除了所有程式庫支援的環境變數外,Ruby
程式庫還有兩個其他變數:
GOOGLE_ADS_RUBY_LOG_LEVEL
:如上述 log_level
。
GOOGLE_ADS_RUBY_HTTP_PROXY
:指定 HTTP Proxy。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-21 (世界標準時間)。
[null,null,["上次更新時間:2025-08-21 (世界標準時間)。"],[[["\u003cp\u003eGoogle Ads API configuration is handled by a \u003ccode\u003eConfig\u003c/code\u003e class, modifiable via configuration files, dynamic settings, or environment variables.\u003c/p\u003e\n"],["\u003cp\u003eYou can provide a \u003ccode\u003egoogle_ads_config.rb\u003c/code\u003e file path directly, set it using the \u003ccode\u003eGOOGLE_ADS_CONFIGURATION_FILE_PATH\u003c/code\u003e environment variable, or the library defaults to your home directory.\u003c/p\u003e\n"],["\u003cp\u003eDynamic configuration allows setting or updating values using blocks when instantiating or after creating the \u003ccode\u003eGoogle::Ads::GoogleAds::GoogleAdsClient\u003c/code\u003e object.\u003c/p\u003e\n"],["\u003cp\u003eEnvironment variables offer another configuration approach, loaded by calling \u003ccode\u003eload_environment_config\u003c/code\u003e on the client instance, overriding existing settings.\u003c/p\u003e\n"],["\u003cp\u003eSupported configuration fields include OAuth credentials, developer token, login customer ID, logging preferences, and a proxy setting specific to the Ruby library.\u003c/p\u003e\n"]]],[],null,["# Configuration is done using a `Config` class. You can access this class in multiple\nways, such as writing a configuration file, specifying values on creation, or\nloading values from environment variables.\n\nConfiguration file\n------------------\n\nYou can specify a\n[`google_ads_config.rb`](https://github.com/googleads/google-ads-ruby/blob/HEAD/google_ads_config.rb)\nfile to use when instantiating the client.\n\nIf you use no arguments when instantiating: \n\n client = Google::Ads::GoogleAds::GoogleAdsClient.new\n\nthen the library will look in the location specified in the\n`GOOGLE_ADS_CONFIGURATION_FILE_PATH` environment variable. If that variable is\nnot set, then the library will look in your `HOME` directory for the file.\n\nAlternatively, you can specify a path: \n\n client = Google::Ads::GoogleAds::GoogleAdsClient.new(\"path/to/file.rb\")\n\nin which case the client will look for the file at that filepath.\n\nThe easiest way to generate this file is to copy the `google_ads_config.rb` from\nthe GitHub repository and modify it to include your refresh token, client ID,\nand client secret.\n\nDynamic configuration\n---------------------\n\nYou can set up the configuration dynamically when instantiating the library,\nor even afterwards: \n\n client = Google::Ads::GoogleAds::GoogleAdsClient.new do |config|\n config.client_id = 'INSERT_CLIENT_ID_HERE'\n # ... more configuration\n end\n\nYou can even modify the configuration after instantiation: \n\n client.configure do |config|\n config.login_customer_id = 'INSERT_LOGIN_CUSTOMER_ID_HERE'\n # ... more configuration\n end\n\nConfiguration fields\n--------------------\n\nThe `Config` object supports the following fields.\n\nGeneral fields:\n\n- `refresh_token`: Your OAuth refresh token.\n- `client_id`: Your OAuth client ID.\n- `client_secret`: Your OAuth client secret.\n- `developer_token`: Your developer token for accessing the API.\n- `login_customer_id`: See the [login-customer-id documentation](/google-ads/api/docs/concepts/call-structure#cid).\n\nLogging fields. See the [logging guide](/google-ads/api/docs/client-libs/ruby/logging) for complete details.\n\n- `log_level`: The minimum log level messages you want logged. For example, specifying `'DEBUG'` will ensure you see all log messages, and specifying `'INFO'` will exclude DEBUG messages but show all other messages.\n- `log_target`: Where you want to log to, such as `STDERR`.\n- `logger`: Specify your own custom logger. Specifying this will override both `log_level` and `log_target`.\n\nEnvironment variables\n---------------------\n\nYou can configure the client library using environment variables. They are not\nloaded by default, and require an additional call after instantiating the\nclient in order to load. See the complete list of [environment variables\nsupported by all\nlanguages](/google-ads/api/docs/client-libs#configuration).\n\nTo load the environment variables into your `Config`, call `load_environment_config`: \n\n client = Google::Ads::GoogleAds::GoogleAdsClient.new\n client.load_environment_config\n\nEach time this is called, the current state of the environment variables will\noverwrite anything that is already present in the `Config`. With this\nmechanism, you can for example have a default config but override specific\nvalues using environment variables, or you can entirely configure with\nenvironment variables.\n\nIn addition to the environment variables supported by all libraries, the Ruby\nlibrary has two additional variables:\n\n- `GOOGLE_ADS_RUBY_LOG_LEVEL`: As `log_level` above.\n- `GOOGLE_ADS_RUBY_HTTP_PROXY`: Specify an HTTP proxy."]]