/google-ads-dotnet

Primary LanguageC#Apache License 2.0Apache-2.0

Google Ads Client Library for .NET

This project hosts the .NET client library for the Google Ads API.

Build Status

Build Status

Features

  • Stub classes for all the supported API versions and services.
  • Easy management of credentials, authentication, and session information.
  • Docs available in HTML and XML format.

Supported Frameworks

  • .NET Framework 4.5.2+
  • .NET Standard 2.0

Announcements and updates

For API and client library updates and news, please follow our Google Ads Developers blog: http://googleadsdeveloper.blogspot.com/.

Running the code examples

We provide a zip package for the examples. To run the code examples, follow these steps:

  1. Download the Google.Ads.GoogleAds.Examples zip file from https://github.com/googleads/google-ads-dotnet/releases/latest.
  2. Extract the examples zip file into a local folder.
  3. Open `Google.Ads.GoogleAds.Examples.sln in Visual Studio and build it.
  4. Follow the instructions on the Authentication guide to create an OAuth2 client ID and Secret.
  5. Run the AuthenticateInStandaloneApplication code example from the Visual Studio project. When prompted, provide the client ID and client secret from the previous step. AuthenticateInStandaloneApplication performs the authentication and prints the configuration in XML format. Copy the configuration into the App.config of the Google.Ads.GoogleAds.Examples project.
  6. Open the Properties dialog for the Examples project by right clicking Google.Ads.GoogleAds.Examples project in the Solution Explorer and selecting the Properties option from the context menu.
  7. Navigate to the Debug Tab and enter the command-line options, which are of the form version.examplename. For instance, V0.GetCampaigns is the command-line option to run GetCampaigns in the V0 version of the Google Ads API.
  8. Compile and run the Google.Ads.GoogleAds.Examples project.

As an alternative to steps 8-10, each code example has a main() method, so you can set the appropriate code example as the Startup object (Select the Application tab on the Examples Properties dialog and pick the desired class from the "Startup object" dropdown.).

Using the client library in your own project

The client library binaries are distributed via Nuget. Add a Nuget reference to the Google.Ads.GoogleAds package in your project to use the client library.

How do I make API calls?

// Create a Google Ads client.
GoogleAdsClient client = new GoogleAdsClient();

// Create the required service.
CampaignServiceClient campaignService =
    client.GetService(Services.V0.CampaignService);

// make more calls to service class.

See the Getting Started guide for more details, and examples folder for code examples that show how to use various services.

How do I enable logging?

You can enable logging by adding the following line in your Program.cs before making any API calls.

TraceUtilities.Configure(TraceUtilities.DETAILED_REQUEST_LOGS_SOURCE,
    "C:\\logs\\details.log", System.Diagnostics.SourceLevels.All);

In most cases, the logs generated by the client library should provide enough details to troubleshoot your issues. When reaching out to the support forum / aliases, you can either provide the logs (which redacts sensitive information by default), or just share the request ID (which is logged as part of the response log).

If you prefer capturing the request ID yourself, then you need to provide a GoogleResponseMetadata object to capture the request ID, as follows:

GoogleAdsResponseMetadata metadata = new GoogleAdsResponseMetadata(client.Config);

// Add the campaigns.
MutateCampaignsResponse retVal = campaignService.MutateCampaigns(
    customerId.ToString(), operations.ToArray(), metadata.CallSettings);

Console.WriteLine(metadata.RequestId);

If the API log doesn't give you enough details, then you can enable more low level logging at grpc level. Keep in mind that this can get pretty detailed and long. The grpc logs are written to stderr, but you can attach your own logger like shown below. You can find all the supported environment variables here: https://github.com/grpc/grpc/blob/master/doc/environment_variables.md

Environment.SetEnvironmentVariable("GRPC_VERBOSITY", "DEBUG");
Environment.SetEnvironmentVariable("GRPC_TRACE", "http");
GrpcEnvironment.SetLogger(new ConsoleLogger());

Miscellaneous

Wiki

Issue tracker

API Documentation

Support forum

Authors