如要透過 HTTP 或 HTTPS Proxy 連線至 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 指定為 Proxy。
以程式輔助方式設定
您也可以使用 GoogleAdsClient 初始化方法 (load_from_dict、load_from_env 和 load_from_string),以程式輔助方式設定 Proxy 設定,就像其他設定一樣:
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 方法設定 Proxy,請設定 GOOGLE_ADS_HTTP_PROXY 環境變數:
export GOOGLE_ADS_HTTP_PROXY="http://user:pass@localhost:8082"