使用 Google Ad Manager API(Beta 版)可以读取您的 Ad Manager 数据并生成报告。
如需发出您的第一个 API 请求,请完成以下步骤:
获取对 Ad Manager 广告资源网的访问权限
如果您还没有 Ad Manager 账号,请注册一个。
启用 Ad Manager API
在您的 Google API 控制台项目中启用 Ad Manager API。
身份验证
所有 API 请求都必须使用 OAuth2 进行身份验证。
Ad Manager API 客户端库会从应用默认凭据读取凭据。您可以使用环境变量或 gcloud 进行设置。
服务账号
Linux 或 macOS
export GOOGLE_APPLICATION_CREDENTIALS=KEY_FILE_PATH
Windows
set GOOGLE_APPLICATION_CREDENTIALS=KEY_FILE_PATH
用户凭据
gcloud auth application-default login --scopes="https://www.googleapis.com/auth/admanager"
# End user credentials must specify the cloud project where the API is enabled.
gcloud auth application-default set-quota-project PROJECT_ID
如需详细了解如何选择凭据类型和创建凭据,请参阅身份验证指南。
设置客户端库
Java
对于 Maven:
<!-- pom.xml -->
<dependency>
<groupId>com.google.api-ads</groupId>
<artifactId>ad-manager</artifactId>
<version>0.1.0</version>
</dependency>
对于 Gradle:
implementation 'com.google.api-ads:ad-manager:0.1.0'
Python
从 PyPi 安装客户端库。
pip install google-ads-admanager
.NET
从 NuGet 安装客户端库。
通过 .NET CLI:
dotnet add package Google.Ads.AdManager.V1 --version 1.0.0-beta01
作为软件包引用:
<PackageReference Include="Google.Ads.AdManager.V1" Version="1.0.0-beta01" />
发出第一个请求
Java
import com.google.ads.admanager.v1.GetNetworkRequest;
import com.google.ads.admanager.v1.Network;
import com.google.ads.admanager.v1.NetworkName;
import com.google.ads.admanager.v1.NetworkServiceClient;
public class SyncGetNetwork {
public static void main(String[] args) throws Exception {
syncGetNetwork();
}
public static void syncGetNetwork() throws Exception {
try (NetworkServiceClient networkServiceClient = NetworkServiceClient.create()) {
GetNetworkRequest request =
GetNetworkRequest.newBuilder()
.setName(NetworkName.of("[NETWORK_CODE]").toString())
.build();
Network response = networkServiceClient.getNetwork(request);
}
}
}
Python
from google.ads import admanager_v1
def sample_get_network():
# Create a client
client = admanager_v1.NetworkServiceClient()
# Initialize request argument(s)
request = admanager_v1.GetNetworkRequest(
name="name_value",
)
# Make the request
response = client.get_network(request=request)
# Handle the response
print(response)
.NET
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);
}
}
cURL
curl -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \
https://admanager.googleapis.com/v1/networks/NETWORK_CODE