代理配置

如果您需要通过 HTTP 或 HTTPS 代理连接到 Google Ads API,可以在配置文件中、以编程方式或通过环境变量配置 http_proxy 设置。http_proxy 设置适用于 Python 客户端库使用的底层 gRPC 传输渠道。

使用 google-ads.yaml 进行配置

在 google-ads.yaml 文件中指定 http_proxy 设置:

# Optional proxy configuration used for API requests. If your proxy does not
# require authentication, specify only the host and port.
http_proxy: INSERT_PROXY_HERE

例如,您可以将 http://user:pass@localhost:8082 指定为代理。

以程序化方式配置

您还可以使用 GoogleAdsClient 初始化方法(load_from_dict、load_from_env 和 load_from_string)以编程方式配置代理设置,就像配置任何其他配置设置一样:

from google.ads.googleads.client import GoogleAdsClient

config = {
    # Include other required configuration settings here.
    "http_proxy": "INSERT_PROXY_HERE",
}
googleads_client = GoogleAdsClient.load_from_dict(config)

使用环境变量进行配置

如需使用 load_from_env 方法配置代理,请设置 GOOGLE_ADS_HTTP_PROXY 环境变量:

export GOOGLE_ADS_HTTP_PROXY="http://user:pass@localhost:8082"