.NET
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
Google 提供了一个 .NET 客户端库,用于与 Ad Manager API 进行交互。我们建议将客户端库与 NuGet 搭配使用。
首先,在您选择的 IDE 中创建一个新项目,或将依赖项添加到现有项目。Google 会将客户端库工件作为 Google.Ads.AdManager.V1
发布到 NuGet 代码库。
软件包引用
<!-- 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
.NET 客户端库使用 OAuth2 和应用默认凭据 (ADC) 进行身份验证。
ADC 会按顺序在以下位置搜索凭据:
GOOGLE_APPLICATION_CREDENTIALS
环境变量。
- 通过 Google Cloud CLI (gcloud CLI) 设置的用户凭据。
- 在 Google Cloud 上运行时,请使用关联到 Google Cloud 资源的服务账号。
如需创建和配置 ADC 凭据,请参阅身份验证。
提交第一个请求
每项服务都有一个 ServiceClient
对象,其中包含适用于每种 REST 方法的同步和异步方法。以下示例会同步读取 Network
。所有实体都通过资源名称进行标识,该名称由实体的类型和数字标识符组成。
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);
}
}
如需查看其他方法和资源的示例,请参阅 GitHub 代码库 googleapis/google-cloud-dotnet
。
处理错误
在 .NET 客户端库中,所有 Ad Manager API 错误均为 Grpc.Core.RpcException 类型。
错误包含唯一的 request_id
,您可以将其提供给支持团队,以寻求排查问题方面的帮助。以下示例会提取 HTTP 状态、错误消息和 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";
}
构造资源名称
客户端库提供了一些辅助类,用于根据 ID 构建资源名称。这些参数可以直接传递给服务方法。
// Represents a resource name in the format:
// "networks/{networkCode}/orders/{orderId}"
OrderName name = OrderName.FromNetworkCodeOrder("123", "456");
Order response = orderServiceClient.GetOrder(name);
.NET 客户端库会遵循 HttpClient.DefaultProxy
属性,包括通过环境变量 HTTP_PROXY
和 HTTPS_PROXY
设置代理。如需了解详情,请参阅 HttpClient.DefaultProxy 属性。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-09-02。
[null,null,["最后更新时间 (UTC):2025-09-02。"],[[["\u003cp\u003eGoogle provides a .NET client library, accessible via NuGet, for seamless interaction with the Ad Manager API.\u003c/p\u003e\n"],["\u003cp\u003eThe library utilizes Application Default Credentials (ADC) for authentication, prioritizing environment variables, Google Cloud CLI credentials, and service accounts.\u003c/p\u003e\n"],["\u003cp\u003eDevelopers can leverage \u003ccode\u003eServiceClient\u003c/code\u003e objects with synchronous and asynchronous methods to access various Ad Manager resources, such as networks.\u003c/p\u003e\n"],["\u003cp\u003eError handling is facilitated through Grpc.Core.RpcException, providing access to status codes, messages, and request IDs for troubleshooting.\u003c/p\u003e\n"],["\u003cp\u003eResource names can be easily constructed using helper classes, enabling efficient interaction with specific Ad Manager entities.\u003c/p\u003e\n"]]],["The Google Ad Manager API .NET client library, available via NuGet as `Google.Ads.AdManager.V1`, uses OAuth2 and Application Default Credentials (ADC) for authentication. To make requests, create a `ServiceClient` object, and utilize synchronous or asynchronous methods. Resource names, formatted with IDs, can be constructed using helper classes. Error handling involves catching `RpcException`, retrieving the status code, message, and `request_id`. The library also supports proxy configuration through the `HttpClient.DefaultProxy` property and environment variables.\n"],null,["Google provides a .NET client library for interacting with the Ad Manager API.\nWe recommend using the client library with NuGet.\n\nTo get started, create a new project in the IDE of your choice or add the\ndependency to an existing project. Google publishes client library artifacts to\nNuGet repository as\n[`Google.Ads.AdManager.V1`](//www.nuget.org/packages/Google.Ads.AdManager.V1). \n\nPackage Reference \n\n \u003c!-- MyProject.csproj --\u003e\n \u003cPackageReference Include=\"Google.Ads.AdManager.V1\" Version=\"1.0.0-beta01\" /\u003e\n\n.NET CLI \n\n dotnet add package Google.Ads.AdManager.V1 --version 1.0.0-beta01\n\nConfigure credentials\n\nThe .NET client library uses OAuth2 and [Application Default Credentials](//cloud.google.com/docs/authentication/application-default-credentials)\n(ADC) to authenticate.\n\nADC searches for credentials in order in the following locations:\n\n1. `GOOGLE_APPLICATION_CREDENTIALS` environment variable.\n2. User credentials set up through the Google Cloud CLI (gcloud CLI).\n3. When running on Google Cloud, the service account attached to the Google Cloud resource.\n\nFor creating and configuring your ADC credentials, see\n[Authentication](/ad-manager/api/beta/authentication).\n\nMake your first request\n\nEach service has a `ServiceClient` object with both synchronous and asynchronous\nmethods for each REST method. The following example reads a [`Network`](/ad-manager/api/beta/reference/rest/v1/networks)\nsynchronously. All entities are identified by a\n[resource name](#construct_resource_names) consisting of the type and numeric\nidentifier of the entity. \n\n using Google.Ads.AdManager.V1;\n\n public sealed partial class GeneratedNetworkServiceClientSnippets\n {\n public void GetNetwork()\n {\n // Create client\n NetworkServiceClient networkServiceClient = NetworkServiceClient.Create();\n // Initialize request argument(s)\n string name = \"networks/[NETWORK_CODE]\";\n // Make the request\n Network response = networkServiceClient.GetNetwork(name);\n }\n } \n https://github.com/googleapis/google-cloud-dotnet/blob/f7caa4b1d158434eb1cf5c4a185428b6419ee32b/apis/Google.Ads.AdManager.V1/Google.Ads.AdManager.V1.GeneratedSnippets/NetworkServiceClient.GetNetworkSnippet.g.cs#L20-L41\n\nFor examples of other methods and resources, see the GitHub repository\n[`googleapis/google-cloud-dotnet`](//github.com/googleapis/google-cloud-dotnet/apis/Google.Ads.AdManager.V1/Google.Ads.AdManager.V1.GeneratedSnippets).\n\nHandle errors\n\nAll Ad Manager API errors are of the type\n[Grpc.Core.RpcException](//cloud.google.com/dotnet/docs/reference/Grpc.Core/latest/Grpc.Core.RpcException)\nin the .NET client library.\n\nErrors include a unique `request_id` you can\nprovide to [support](/ad-manager/api/beta/support) for assistance with\ntroubleshooting. The following example extracts the HTTP status, error message,\nand `request_id`. \n\n using Google.Ads.AdManager.V1;\n using Google.Api.Gax.Grpc;\n using Google.Rpc;\n using Grpc.Core;\n\n try\n {\n NetworkServiceClient networkServiceClient = NetworkServiceClient.Create();\n Network network = networkServiceClient.GetNetwork(\"networks/[NETWORK_CODE]\");\n }\n catch(RpcException e)\n {\n // HTTP Status code\n StatusCode statusCode = e.Status.StatusCode;\n // Error message\n string errorMessage = e.Status.Detail;\n // Unique request identifier.\n RequestInfo requestInfo = e.GetStatusDetail\u003cRequestInfo\u003e();\n string requestId = requestInfo?.RequestId ?? \"Unexpected null request identifier\";\n }\n\nConstruct resource names\n\nThe client library provides helper classes for building resource names from\nIDs. These can be passed directly to service methods. \n\n // Represents a resource name in the format:\n // \"networks/{networkCode}/orders/{orderId}\"\n OrderName name = OrderName.FromNetworkCodeOrder(\"123\", \"456\");\n Order response = orderServiceClient.GetOrder(name);\n\nConfigure proxy settings\n\nThe .NET client library respects the `HttpClient.DefaultProxy` property\nincluding setting proxies from the environment variables `HTTP_PROXY` and\n`HTTPS_PROXY`. For more details, see\n[HttpClient.DefaultProxy Property](//learn.microsoft.com/en-us/dotnet/api/system.net.http.httpclient.defaultproxy)."]]