开始使用

您可以使用 Google Ad Manager SOAP API 构建用于管理广告资源、创建订单、提取报告等的应用。

为帮助您起步,我们为客户 适用于 Java、.NET、Python、PHP 和 Ruby 语言的库

如需发出第一个 API 请求,请按以下步骤操作:

获取对 Ad Manager 广告资源网的访问权限

如果您还没有 Ad Manager 账号,请注册 Ad Manager 账号。您还可以创建一个 如果您想在测试环境中测试 API,请 独立环境请注意,您无需通过 AdSense 账号进行测试 目的。

记下您的广告资源网代码。登录后,您可以在网址中找到此 ID 您的广告联盟例如,在网址中 https://admanager.google.com/1234#home1234 是您的广告资源网代码。

创建身份验证凭据

您必须使用 OAuth 2.0 对所有 Ad Manager SOAP API 请求进行身份验证。以下步骤介绍了访问您自己的 Ad Manager 数据的用例。如需了解详情和其他选项,请参阅身份验证

  1. 打开 Google API 控制台 “凭据”页面

  2. 从项目菜单中选择创建项目,输入项目名称,并根据需要修改提供的项目 ID。点击创建

  3. 在“凭据”页面上,选择创建凭据,然后选择服务账号密钥

  4. 选择新的服务账号,然后选择 JSON 作为密钥类型。

  5. 点击创建以下载包含私钥的文件。

配置您的 Ad Manager 广告资源网

  1. 登录 Google Ad Manager。

  2. 在边栏中,点击管理 >全局设置

  3. 常规设置 > API 访问权限下,将滑块滑动到已启用

  4. 点击页面底部的保存按钮。

设置您的客户端

下载 Ad Manager 客户端库之一。通过 库提供封装容器函数和功能,以便您更轻松、更快速地 开发应用。

以下标签页提供了使用客户端库提供的每种语言进行编码的快速入门。

Java

以下是一个基本示例,展示了如何使用 Java 客户端库。如需更详细的使用信息,请参阅 README 文件。

  1. 设置凭据

    在 shell 中运行以下命令:

    curl https://raw.usercontent.com/googleads/googleads-java-lib/main/examples/admanager_axis/src/main/resources/ads.properties -o ~/ads.properties
    打开 ~/ads.properties 文件并填充以下字段:
    [...]
    api.admanager.applicationName=INSERT_APPLICATION_NAME_HERE
    api.admanager.jsonKeyFilePath=INSERT_PATH_TO_JSON_KEY_FILE_HERE
    api.admanager.networkCode=INSERT_NETWORK_CODE_HERE
    [...]
  2. 指定依赖项

    修改 pom.xml 文件,并将以下代码添加到 dependencies 标记。您可以在 上找到最新版本号

    <dependency>
      <groupId>com.google.api-ads</groupId>
      <artifactId>ads-lib</artifactId>
      <version>RELEASE</version>
    </dependency>
    <dependency>
      <groupId>com.google.api-ads</groupId>
      <artifactId>dfp-axis</artifactId>
      <version>RELEASE</version>
    </dependency>
  3. 编写一些代码并发出请求!

    import com.google.api.ads.common.lib.auth.OfflineCredentials;
    import com.google.api.ads.common.lib.auth.OfflineCredentials.Api;
    import com.google.api.ads.admanager.axis.factory.AdManagerServices;
    import com.google.api.ads.admanager.axis.v202505.Network;
    import com.google.api.ads.admanager.axis.v202505.NetworkServiceInterface;
    import com.google.api.ads.admanager.lib.client.AdManagerSession;
    import com.google.api.client.auth.oauth2.Credential;
    
    public class App {
      public static void main(String[] args) throws Exception {
        Credential oAuth2Credential = new OfflineCredentials.Builder()
            .forApi(Api.AD_MANAGER)
            .fromFile()
            .build()
            .generateCredential();
    
        // Construct an AdManagerSession.
        AdManagerSession session = new AdManagerSession.Builder()
            .fromFile()
            .withOAuth2Credential(oAuth2Credential)
            .build();
    
        // Construct a Google Ad Manager service factory, which can only be used once per
        // thread, but should be reused as much as possible.
        AdManagerServices adManagerServices = new AdManagerServices();
    
        // Retrieve the appropriate service
        NetworkServiceInterface networkService = adManagerServices.get(session,
            NetworkServiceInterface.class);
    
        // Make a request
        Network network = networkService.getCurrentNetwork();
    
        System.out.printf("Current network has network code '%s' and display" +
            " name '%s'.%n", network.getNetworkCode(), network.getDisplayName());
      }
    }

Python

以下是一个基本示例,展示了如何使用 Python 客户端库。 Python 客户端库支持 Python 3.6 及更高版本。如需详细了解使用方式 参阅 README 文件。

  1. 安装该库并设置凭据。

    在 shell 中运行以下命令:

    pip install googleads
    curl https://raw.usercontent.com/googleads/googleads-python-lib/main/googleads.yaml \
         -o ~/googleads.yaml
  2. 设置您的 ~/googleads.yaml 文件。

    填写以下字段:

    ad_manager:
      application_name: INSERT_APPLICATION_NAME_HERE
      network_code: INSERT_NETWORK_CODE_HERE
      path_to_private_key_file: INSERT_PATH_TO_FILE_HERE
  3. 运行一些代码并发出请求。
    # Import the library.
    from googleads import ad_manager
    
    # Initialize a client object, by default uses the credentials in ~/googleads.yaml.
    client = ad_manager.AdManagerClient.LoadFromStorage()
    
    # Initialize a service.
    network_service = client.GetService('NetworkService', version='v202505')
    
    # Make a request.
    current_network = network_service.getCurrentNetwork()
    
    print("Current network has network code '%s' and display name '%s'." %
            (current_network['networkCode'], current_network['displayName']))

PHP

下面是一个基本示例,介绍如何使用 PHP 客户端库

  1. 安装库并设置凭据。

    在 Shell 中运行以下命令,安装客户端库并下载 adsapi_php.ini 文件复制到您的主目录中:

    composer require googleads/googleads-php-lib
    curl https://raw.usercontent.com/googleads/googleads-php-lib/main/examples/AdManager/adsapi_php.ini -o ~/adsapi_php.ini
  2. 设置 ~/adsapi_php.ini 文件。

    填写以下字段:

    [AD_MANAGER]
    networkCode = "INSERT_NETWORK_CODE_HERE"
    applicationName = "INSERT_APPLICATION_NAME_HERE"
    
    [OAUTH2]
    jsonKeyFilePath = "INSERT_ABSOLUTE_PATH_TO_OAUTH2_JSON_KEY_FILE_HERE"
    scopes = "https://www.googleapis.com/auth/dfp"
  3. 运行一些代码并发出请求!
    <?php
    require 'vendor/autoload.php';
    use Google\AdsApi\AdManager\AdManagerSession;
    use Google\AdsApi\AdManager\AdManagerSessionBuilder;
    use Google\AdsApi\AdManager\v202505\ApiException;
    use Google\AdsApi\AdManager\v202505\ServiceFactory;
    use Google\AdsApi\Common\OAuth2TokenBuilder;
    
    // Generate a refreshable OAuth2 credential for authentication.
    $oAuth2Credential = (new OAuth2TokenBuilder())
        ->fromFile()
        ->build();
    // Construct an API session configured from a properties file and the OAuth2
    // credentials above.
    $session = (new AdManagerSessionBuilder())
        ->fromFile()
        ->withOAuth2Credential($oAuth2Credential)
        ->build();
    
    // Get a service.
    $serviceFactory = new ServiceFactory();
    $networkService = $serviceFactory->createNetworkService($session);
    
    // Make a request
    $network = $networkService->getCurrentNetwork();
    printf(
        "Network with code %d and display name '%s' was found.\n",
        $network->getNetworkCode(),
        $network->getDisplayName()
    );

.NET

下面是一个基本示例,介绍如何使用 .NET 客户端 库

  1. 创建新项目

    打开 Visual Studio 并创建一个新项目(控制台应用)。

  2. 为项目添加所需的库引用

    Google.Dfp 添加 nuget 依赖项。

  3. 设置 App.config

    将 src\App.config 复制到项目目录,然后将其添加到项目中。如果您的应用有自己的 App.config,那么您可以将以下节点复制到 App.config 中:

    • configuration/AdManagerApi
    • configuration/configSections/section[name=&quot;AdManagerApi&quot;]
    • configuration/system.net
  4. 设置凭据

    打开 App.config 并修改以下键:

    <add key="ApplicationName" value="INSERT_YOUR_APPLICATION_NAME_HERE" />
    <add key="NetworkCode" value="INSERT_YOUR_NETWORK_CODE_HERE" />
    <add key="OAuth2Mode" value="SERVICE_ACCOUNT" />
    <add key="OAuth2SecretsJsonPath" value="INSERT_OAUTH2_SECRETS_JSON_FILE_PATH_HERE" />
  5. 调用库

    您可以调用该库,如以下 C# 代码段所示

    AdManagerUser user = new AdManagerUser();
          using (InventoryService inventoryService = user.GetService<InventoryService>())
                {
                    // Create a statement to select ad units.
                    int pageSize = StatementBuilder.SUGGESTED_PAGE_LIMIT;
                    StatementBuilder statementBuilder =
                        new StatementBuilder().OrderBy("id ASC").Limit(pageSize);
    
                    // Retrieve a small amount of ad units at a time, paging through until all
                    // ad units have been retrieved.
                    int totalResultSetSize = 0;
                    do
                    {
                        AdUnitPage page =
                            inventoryService.getAdUnitsByStatement(statementBuilder.ToStatement());
    
                        // Print out some information for each ad unit.
                        if (page.results != null)
                        {
                            totalResultSetSize = page.totalResultSetSize;
                            int i = page.startIndex;
                            foreach (AdUnit adUnit in page.results)
                            {
                                Console.WriteLine(
                                    "{0}) Ad unit with ID \"{1}\" and name \"{2}\" was found.", i++,
                                    adUnit.id, adUnit.name);
                            }
                        }
    
                        statementBuilder.IncreaseOffsetBy(pageSize);
                    } while (statementBuilder.GetOffset() < totalResultSetSize);
    
                    Console.WriteLine("Number of results found: {0}", totalResultSetSize);
                }
            

如果您不想在 App.config 中设置凭据,请参阅这篇 Wiki 文章,了解使用 AdManagerUser 类的替代方法。如需详细了解如何使用 .NET 客户端库,请参阅自述文件。如果您想在不使用客户端库的情况下在 .NET 中进行开发,请参阅 NoClientLibrary 维基文章

Ruby

下面是一个基本示例,介绍如何使用 Ruby 客户端库。Ruby 客户端库要求使用 Ruby 2.1 或更高版本。

  1. 安装 Ruby gem 并获取配置文件。

    在 shell 中运行以下命令:

    gem install google-dfp-api
    curl https://raw.usercontent.com/googleads/google-api-ads-ruby/main/ad_manager_api/ad_manager_api.yml -o ~/ad_manager_api.yml
  2. 设置凭据

    填写 ~/ad_manager_api.yml 文件中的必填字段。如果您还没有 OAuth2 密钥文件,则需要按照相应步骤创建 OAuth2 凭据

    :authentication:
      :oauth2_keyfile: INSERT_PATH_TO_JSON_KEY_FILE_HERE
      :application_name: INSERT_APPLICATION_NAME_HERE
      :network_code: INSERT_NETWORK_CODE_HERE
  3. 编写一些代码并发出请求!
    # Import the library.
    require 'ad_manager_api'
    
    # Initialize an Ad Manager client instance (uses credentials in ~/ad_manager_api.yml by default).
    ad_manager = AdManagerApi::Api.new
    
    # Get a service instance.
    network_service = ad_manager.service(:NetworkService, :v202505)
    
    # Make a request.
    network = network_service.get_current_network()
    
    puts "The current network is %s (%d)." %
            [network[:display_name], network[:network_code]]

如需详细的入门步骤,请参阅README 该文件随 Ruby 客户端库一起分发。另外,请查看我们的完整 Ruby 版示例库

后续步骤

客户端库运行起来后,请修改提供的示例,以根据您的需求对其进行扩展。

浏览参考文档,详细了解该 API。

如果您需要帮助,请访问我们的支持页面