.NET

Google provides a .NET client library for interacting with the Ad Manager API. We recommend using the client library with NuGet.

To get started, create a new project in the IDE of your choice or add the dependency to an existing project. Google publishes client library artifacts to NuGet repository as Google.Ads.AdManager.V1.

Package Reference

<!-- MyProject.csproj -->
<PackageReference Include="Google.Ads.AdManager.V1" Version="1.0.0-beta01" />

.NET CLI

dotnet add package Google.Ads.AdManager.V1 --version 1.0.0-beta01

Configure credentials

The .NET client library uses OAuth2 and Application Default Credentials (ADC) to authenticate.

ADC searches for credentials in order in the following locations:

  1. GOOGLE_APPLICATION_CREDENTIALS environment variable.
  2. User credentials set up through the Google Cloud CLI (gcloud CLI).
  3. When running on Google Cloud, the service account attached to the Google Cloud resource.

For creating and configuring your ADC credentials, see Authentication.

Make your first request

Each service has a ServiceClient object with both synchronous and asynchronous methods for each REST method. The following example reads a Network synchronously. All entities are identified by a resource name consisting of the type and numeric identifier of the entity.

    using Google.Ads.AdManager.V1;

    public sealed partial class GeneratedNetworkServiceClientSnippets
    {
        public void GetNetwork()
        {
            // Create client
            NetworkServiceClient networkServiceClient = NetworkServiceClient.Create();
            // Initialize request argument(s)
            string name = "networks/[NETWORK_CODE]";
            // Make the request
            Network response = networkServiceClient.GetNetwork(name);
        }
    }

For examples of other methods and resources, see the GitHub repository googleapis/google-cloud-dotnet.

Handle errors

All Ad Manager API errors are of the type Grpc.Core.RpcException in the .NET client library.

Errors include a unique request_id you can provide to support for assistance with troubleshooting. The following example extracts the HTTP status, error message, and request_id.

using Google.Ads.AdManager.V1;
using Google.Api.Gax.Grpc;
using Google.Rpc;
using Grpc.Core;

try
{
    NetworkServiceClient networkServiceClient = NetworkServiceClient.Create();
    Network network = networkServiceClient.GetNetwork("networks/[NETWORK_CODE]");
}
catch(RpcException e)
{
    // HTTP Status code
    StatusCode statusCode = e.Status.StatusCode;
    // Error message
    string errorMessage = e.Status.Detail;
    // Unique request identifier.
    RequestInfo requestInfo = e.GetStatusDetail<RequestInfo>();
    string requestId = requestInfo?.RequestId ?? "Unexpected null request identifier";
}

Construct resource names

The client library provides helper classes for building resource names from IDs. These can be passed directly to service methods.

//  Represents a resource name in the format:
//  "networks/{networkCode}/orders/{orderId}"
OrderName name = OrderName.FromNetworkCodeOrder("123", "456");
Order response = orderServiceClient.GetOrder(name);