Google Ads API 用戶端程式庫提供多種設定,可供您自訂程式庫行為。
在執行階段設定程式庫
建議您在執行階段初始化 GoogleAdsConfig
物件,藉此設定用戶端程式庫:
GoogleAdsConfig config = new GoogleAdsConfig()
{
DeveloperToken = "******",
OAuth2Mode = OAuth2Flow.APPLICATION,
OAuth2ClientId = "******.apps.googleusercontent.com",
OAuth2ClientSecret = "******",
OAuth2RefreshToken = "******"
};
GoogleAdsClient client = new GoogleAdsClient(config);
其他設定選項
我們也提供一些額外選項來設定用戶端程式庫:如要啟用這些選項,請在專案中將 Nuget 參照新增至 Google.Ads.GoogleAds.Extensions
套件。
如果您使用其中一個選項,系統不會自動擷取設定,您應明確載入設定,如下所示。
使用 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 中的建構規則
最後,在專案中加入新的設定檔。將這個檔案的屬性變更為「一律複製到輸出資料夾」。
現在請建構並執行專案。應用程式會開始從新的設定檔中擷取值。
使用自訂 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。
調整
MaxReceiveMessageLengthInBytes
和MaxMetadataSizeInBytes
設定,修正特定ResourceExhausted
錯誤。這些設定可解決Status(StatusCode="ResourceExhausted",Detail="Received message larger than max (423184132 versus 67108864)"
形式的錯誤。在這個範例中,錯誤是因訊息大小 (
423184132 bytes
) 大於程式庫可處理的大小 (67108864 bytes
) 所致。請將MaxReceiveMessageLengthInBytes
增加至500000000
,避免發生這項錯誤。請注意,這項錯誤也表示程式碼處理了極大的 Response 物件 (例如大型
SearchGoogleAdsResponse
)。由於 .NET 的「大型物件堆積」,這可能會對程式碼效能造成影響。如果這成為效能問題,您可能需要瞭解如何重組 API 呼叫或重新設計部分應用程式。
OAuth2 設定
使用 OAuth2 授權對 Google Ads API 伺服器發出的呼叫時,請設定下列設定金鑰:
AuthorizationMethod
:設為OAuth2
。OAuth2Mode
:設為APPLICATION
或SERVICE_ACCOUNT
。OAuth2ClientId
:將這個值設為 OAuth2 用戶端 ID。OAuth2ClientSecret
:將這個值設為 OAuth 2.0 用戶端密鑰。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 使用者介面中的已連結帳戶授權時,只有在更新實體資源的方法中,才需要這個標頭 (Google Ads API 中的AccountLink
資源)。將這個值設為資料供應商的客戶 ID,該供應商會更新指定客戶 ID 的資源。設定時請勿使用連字號 (-
)。進一步瞭解連結帳戶。