Ads API Client library provides several configuration settings that you can use to customize the library behavior.

Configuring using App.config

All the settings specific to Google Ads API are stored in the GoogleAdsApi node of the App.config file. A typical configuration App.config might like follows:

<?xml version="1.0" encoding="utf-8"?>
    <section name="GoogleAdsApi" type="System.Configuration.DictionarySectionHandler" />
    <!-- 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 = "******" />
    <add key = "OAuth2ClientSecret" value = "******" />
    <add key = "OAuth2RefreshToken" value = "******" />
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />

Specifying a separate App.config file

If you don't want to get your App.config cluttered, you could move the library-specific configuration into its own configuration file using the configSource property.

Step 1: Specify a configSource in your App.config

Modify your App.config to look like this:

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

Step 2: Specify the contents of your config file

Now create another config file with the name you specified on configSource, and move the configuration node from your App.config into this file, as follows:

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

Step 3: Fix the build rules in your csproj

Finally, include new configuration file in your project. Change the properties of this file to Always copy to output folder.

Now build and run your project. Your application will start picking up values from the new configuration file.

Configuring using a custom JSON file

You may use an IConfigurationRoot instance to configure the client library. A typical example using JSON would look like follows:

Create a JSON file

Create a JSON file named GoogleAdsApi.json that has similar structure as the App.config file.

    "Timeout": "2000",

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

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

    "OAuth2Mode": "APPLICATION",
    "OAuth2ClientId": "******",
    "OAuth2ClientSecret": "******",
    "OAuth2RefreshToken": "******",

Load the configuration

Next, load the json file into an IConfigurationRoot.

ConfigurationBuilder builder = new ConfigurationBuilder()
IConfigurationRoot configRoot = builder.Build();

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

Configuring using settings.json

The process here is similar to using a custom JSON, except that the keys should be in a section named GoogleAdsApi, like this:

        "DeveloperToken": "******",
        "OAuth2Mode": "APPLICATION",
        "OAuth2ClientId": "******",
        "OAuth2ClientSecret": "******",
        "OAuth2RefreshToken": "******",
    // More settings...

Next, you can use the IConfiguration instance in your page as follows:

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

Configuring the library at runtime.

You may also initialize the GoogleAdsClient at runtime, as shown below:

Example 1: Initialize the configuration at runtime.

GoogleAdsConfig config = new GoogleAdsConfig()
    DeveloperToken = "******",
    OAuth2Mode = "APPLICATION",
    OAuth2ClientId = "******",
    OAuth2ClientSecret = "******",
    OAuth2RefreshToken = "******"

GoogleAdsClient client = new GoogleAdsClient(config);

Example 2: Load from App.config, but override a setting.

GoogleAdsClient client = new GoogleAdsClient(config);
client.Config.DeveloperToken = "******";

Configuration Fields

The following are the list of settings supported by the Google Ads .NET library.

Connectivity settings

  • Timeout: Use this key to set service timeout in milliseconds.
  • ProxyServer: Set this to the HTTP proxy server URL if you are using a proxy to connect to the internet.
  • ProxyUser: Set this to the username you require to authenticate against the proxy server. Leave this empty if a username is not required.
  • ProxyPassword: Set this to the password of ProxyUser if you set a value for ProxyUser.
  • ProxyDomain: Set this to the domain for ProxyUser if your proxy server requires one to be set.

OAuth2 settings

When using OAuth2 to authenticate your calls against the Google Ads API servers, you should set the following configuration keys:

  • AuthorizationMethod: This should be set to OAuth2.
  • OAuth2Mode: This should be set to APPLICATION or SERVICE_ACCOUNT
  • OAuth2ClientId: Set this value to your OAuth2 client id.
  • OAuth2ClientSecret: Set this value to your OAuth2 client secret.
  • OAuth2Scope: Set this value to different scopes if you want to authorize OAuth2 tokens for multiple APIs. This setting is optional.

If you are using OAuth2Mode == APPLICATION, then you need to set the following additional configuration keys.

  • OAuth2RefreshToken: Set this value to a pre-generated OAuth2 refresh token if you wish to reuse OAuth2 tokens. This setting is optional.
  • OAuth2RedirectUri: Set this value to the OAuth2 redirect url. This setting is optional.

See the following guides for more details: * OAuth Installed Application Flow * OAuth Web Application Flow

If you are using OAuth2Mode == SERVICE_ACCOUNT, then you need to set the following additional configuration keys.

  • OAuth2PrnEmail: Set this value to the login email of the account you are impersonating.
  • OAuth2SecretsJsonPath: Set this value to the OAuth2 JSON configuration file path.

See the OAuth Service Account Flow guide for more details.

The following settings are specific to the Google Ads API.

  • DeveloperToken: Set this to your developer token.