設定

Google Ads API 用戶端程式庫提供數種配置設定,可用來自訂程式庫行為。

在執行階段設定程式庫

設定用戶端程式庫的建議做法是在執行階段初始化 GoogleAdsConfig 物件:

GoogleAdsConfig config = new GoogleAdsConfig()
{
    DeveloperToken = "******",
    OAuth2Mode = "APPLICATION",
    OAuth2ClientId = "******.apps.googleusercontent.com",
    OAuth2ClientSecret = "******",
    OAuth2RefreshToken = "******"
};

GoogleAdsClient client = new GoogleAdsClient(config);

替代設定選項

此外,我們還提供幾個設定用戶端程式庫的其他選項:如要啟用,請在專案中的 Google.Ads.GoogleAds.Extensions 套件新增 Nuget 參照。

如果您使用以上其中一個選項,系統不會自動選取配置設定,請依下列說明明確載入設定。

使用 App.config 進行設定

Google Ads API 的所有設定都儲存在 App.config 檔案的 GoogleAdsApi 節點中。一般設定 App.config 如下:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="GoogleAdsApi" type="System.Configuration.DictionarySectionHandler" />
  </configSections>
  <GoogleAdsApi>
    <!-- Set the service timeout in milliseconds. -->
    <add key="Timeout" value="2000" />

    <!-- Proxy settings for library. -->
    <add key="ProxyServer" value="http://localhost:8888"/>
    <add key="ProxyUser" value=""/>
    <add key="ProxyPassword" value=""/>
    <add key="ProxyDomain" value=""/>

    <!-- API-specific settings -->
    <add key="DeveloperToken" value="******"/>

    <!-- OAuth2 settings -->
    <add key = "OAuth2Mode" value="APPLICATION"/>
    <add key = "OAuth2ClientId" value = "******.apps.googleusercontent.com" />
    <add key = "OAuth2ClientSecret" value = "******" />
    <add key = "OAuth2RefreshToken" value = "******" />
  </GoogleAdsApi>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>
</configuration>

如要從 App.config 檔案載入配置設定,請在 GoogleAdsConfig 物件上呼叫 LoadFromDefaultAppConfigSection 方法:

GoogleAdsConfig config = new GoogleAdsConfig();
config.LoadFromDefaultAppConfigSection();
GoogleAdsClient client = new GoogleAdsClient(config);

指定個別的 App.config 檔案

如果不想讓 App.config 看起來雜亂,可以使用 configSource 屬性,將程式庫專屬設定移至自己的設定檔。

步驟 1:在 App.config 中指定 configSource

請將 App.config 修改為以下形式:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="GoogleAdsApi" type="System.Configuration.DictionarySectionHandler"/>
  </configSections>
  <GoogleAdsApi configSource="GoogleAdsApi.config"/>
...
</configuration>

步驟 2:指定設定檔內容

現在,建立另一個在 configSource 指定名稱的設定檔,然後將設定節點從 App.config 移至這個檔案:

<?xml version="1.0" encoding="utf-8" ?>
<GoogleAdsApi>
  ... More settings.
</GoogleAdsApi>

步驟 3:修正 csproj 中的建構規則

最後,請在專案中納入新的設定檔。將這個檔案的屬性變更為「Always copy to output folder」

現在,建構並執行您的專案。您的應用程式將開始從新的設定檔取得值。

使用自訂 JSON 檔案進行設定

您可以使用 IConfigurationRoot 執行個體來設定用戶端程式庫。

建立 JSON 檔案

建立名為 GoogleAdsApi.json 的 JSON 檔案,其結構與 App.config 檔案類似。

{
    "Timeout": "2000",

    "ProxyServer": "http://localhost:8888",
    "ProxyUser": "",
    "ProxyPassword": "",
    "ProxyDomain": "",

    "DeveloperToken": "******",

    "OAuth2Mode": "APPLICATION",
    "OAuth2ClientId": "******.apps.googleusercontent.com",
    "OAuth2ClientSecret": "******",
    "OAuth2RefreshToken": "******",
}

載入設定

接著,將 JSON 檔案載入 IConfigurationRoot

ConfigurationBuilder builder = new ConfigurationBuilder()
    .SetBasePath(Directory.GetCurrentDirectory())
    .AddJsonFile("GoogleAdsApi.json");
IConfigurationRoot configRoot = builder.Build();

GoogleAdsConfig config = new GoogleAdsConfig();
config.LoadFromConfigurationRoot(configRoot);
GoogleAdsClient client = new GoogleAdsClient(config);

使用 settings.json 進行設定

此處的程序與自訂 JSON 類似,但金鑰應位於名為 GoogleAdsApi 的區段中:

{
    "GoogleAdsApi":
    {
        "DeveloperToken": "******",
        "OAuth2Mode": "APPLICATION",
        "OAuth2ClientId": "******.apps.googleusercontent.com",
        "OAuth2ClientSecret": "******",
        "OAuth2RefreshToken": "******",
        ...
    }
    // More settings...
}

接著,您可以在網頁中使用 IConfiguration 執行個體:

IConfigurationSection section = Configuration.GetSection("GoogleAdsApi");
GoogleAdsConfig config = new GoogleAdsConfig();
config.LoadFromConfigurationSection(section);
GoogleAdsClient client = new GoogleAdsClient(config);

使用環境變數進行設定

您也可以使用環境變數初始化 GoogleAdsClient

GoogleAdsConfig config = new GoogleAdsConfig();
config.LoadFromEnvironmentVariables();
GoogleAdsClient client = new GoogleAdsClient(config);

請參閱支援的環境變數完整清單

設定欄位

以下列出 Google Ads .NET 程式庫支援的設定。

連線設定

  • Timeout:使用此金鑰設定服務逾時 (以毫秒為單位)。預設值是根據 googleads_grpc_service_config.json 中的 method_config/timeout 設定而定。如果您需要強制縮短 API 呼叫的時間上限,請設定較低的值。您可以將逾時時間設為 2 小時以上,但 API 仍可能因長時間執行的要求而逾時,並傳回 DEADLINE_EXCEEDED 錯誤。
  • ProxyServer:如果您使用 Proxy 連線至網際網路,請設為 HTTP Proxy 伺服器網址。
  • ProxyUser:將此名稱設為您需要透過 Proxy 伺服器進行驗證時所需的使用者名稱。如果不需要使用者名稱,請將這個欄位留空。
  • ProxyPassword:如果已設定 ProxyUser 的值,請將這個項目設為 ProxyUser 的密碼。
  • ProxyDomain:如果 Proxy 伺服器需要設定,請設為 ProxyUser 的網域。
  • MaxReceiveMessageLengthInBytes:使用這項設定可增加用戶端程式庫可處理的 API 回應大小上限。預設值為 64 MB。
  • MaxMetadataSizeInBytes:使用這項設定可增加用戶端程式庫可處理的 API 錯誤回應大小上限。預設值為 16 MB。

調整 MaxReceiveMessageLengthInBytesMaxMetadataSizeInBytes 設定以修正某些 ResourceExhausted 錯誤。這些設定可以解決 Status(StatusCode="ResourceExhausted",Detail="Received message larger than max (423184132 versus 67108864)" 形式的錯誤。

在此範例中,發生錯誤的原因是訊息大小 (423184132 bytes) 超過程式庫可處理的數量 (67108864 bytes)。請將 MaxReceiveMessageLengthInBytes 提高為 500000000,以免發生這個錯誤。

請注意,此錯誤也表示您的程式碼處理了大幅大型的回應物件 (例如大型 SearchGoogleAdsResponse)。這可能會因為 .NET 的大型物件堆積而影響程式碼效能。如果這會影響效能,您可能必須探索如何重新整理 API 呼叫,或重新設計應用程式的某些部分。

OAuth2 設定

使用 OAuth2 授權對 Google Ads API 伺服器發出的呼叫時,您應設定下列設定金鑰:

  • AuthorizationMethod:設為 OAuth2
  • OAuth2Mode:設為 APPLICATIONSERVICE_ACCOUNT
  • OAuth2ClientId:將這個值設為您的 OAuth2 用戶端 ID。
  • OAuth2ClientSecret:將這個值設為您的 OAuth2 用戶端密鑰。
  • OAuth2Scope:如要為多個 API 授權 OAuth2 憑證,請將這個值設為不同的範圍。這是選用設定。

如果您使用的是 OAuth2Mode == APPLICATION,則必須設定下列其他設定鍵。

  • OAuth2RefreshToken:如果您要重複使用 OAuth2 憑證,請將這個值設為預先產生的 OAuth2 更新權杖。這是選用設定。
  • OAuth2RedirectUri:將這個值設為 OAuth2 重新導向網址。這是選用設定。

詳情請參閱下列指南:

如果您使用的是 OAuth2Mode == SERVICE_ACCOUNT,則必須設定下列額外的設定鍵。

  • OAuth2PrnEmail:將這個值設為要模擬的帳戶電子郵件地址。
  • OAuth2SecretsJsonPath:將這個值設為 OAuth2 JSON 設定檔路徑。

詳情請參閱 OAuth 服務帳戶流程指南。

交通運輸設定

  • UseGrpcCore:將這項設定設為 true,即可使用 Grpc.Core 程式庫做為基礎傳輸層。詳情請參閱 gRPC 支援指南

Google Ads API 設定

下列設定專屬於 Google Ads API。

  • DeveloperToken:將其設為您的開發人員權杖。
  • LoginCustomerId:這是在要求中使用的授權客戶 ID,不含連字號 (-)。
  • LinkedCustomerId:只有在 Google Ads UI 中透過已連結帳戶 (Google Ads API 中的 AccountLink 資源) 授予權限時,更新實體資源的方法才需要此標頭。將這個值設為資料供應商客戶 ID,該供應商的客戶 ID 會更新指定客戶 ID 的資源。設定時不得包含連字號 (-)。進一步瞭解已連結帳戶