配置
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
配置是使用 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
访问 GitHub 代码库并进行修改,在其中添加您的刷新令牌、客户端 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 代理。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-21。
[null,null,["最后更新时间 (UTC):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."]]