瞭解詳情

您可以使用 Google Ad Manager API 建立用來管理廣告空間的應用程式 建立訂單、產生報表等等

Ad Manager API 使用 SOAP。為協助您踏出第一步,我們提供客戶 適用於 Java、.NET、Python、PHP 和 Ruby 的程式庫

如要提出第一個 API 要求,請按照下列步驟操作:

取得 Ad Manager 聯播網的存取權

如果您還沒有 Ad Manager 帳戶,請申請一個 帳戶。你也可以建立 test network (測試網路) 獨立環境請注意,您不需要有 AdSense 帳戶進行測試 用途。

記下您的聯播網代碼。您可在登入後看到這個網址 傳送至您的網路例如,在網址中 https://admanager.google.com/1234#home1234 是你的聯播網代碼。

建立驗證憑證

您必須使用 OAuth 2.0 驗證所有 Ad Manager API 請求。步驟 以下說明瞭存取您自己的 Ad Manager 資料的用途。瞭解詳情 如需其他選項,請參閱驗證

  1. 開啟 Google API 控制台 「憑證」頁面

  2. 從專案選單中選擇 [建立專案],然後輸入 專案,並視需要編輯提供的專案 ID。點選「Create」(建立)

  3. 在「憑證」頁面上選取「建立憑證」,然後選取 服務帳戶金鑰

  4. 選取「新服務」 帳戶 然後選取 JSON 做為金鑰類型

  5. 按一下「建立」,下載內含私密金鑰的檔案。

設定 Ad Manager 聯播網

  1. 登入 Google Ads 經理。

  2. 按一下側欄中的「管理員」「管理員」>通用設定

  3. 在「一般設定」下 > API 存取權按一下滑桿即可啟用

  4. 按一下頁面底部的「Save」按鈕。

設定客戶

下載其中一個 Ad Manager 用戶端程式庫。 程式庫提供包裝函式函式和功能,可更輕鬆快速地 開發應用程式

下列分頁提供快速入門導覽課程,協助您以各種語言,提供編寫程式碼的快速入門導覽課程 這裡有個用戶端程式庫

Java

以下這個基本範例說明如何使用 Java 用戶端 程式庫。如要進一步瞭解使用資訊,請參閱 README 檔案。

  1. 設定憑證

    在殼層中執行下列指令:

    curl https://raw.githubusercontent.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 標記。您可以在 找到最新的版本號碼 GitHub

    <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.v202408.Network;
    import com.google.api.ads.admanager.axis.v202408.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 v3.6+。如需用量詳細資料 請參閱 README 檔案。

  1. 安裝程式庫並設定憑證。

    在殼層中執行下列指令:

    pip install googleads
    curl https://raw.githubusercontent.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='v202408')
    
    # 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. 安裝程式庫並設定憑證。

    在殼層中執行下列指令,安裝用戶端程式庫並下載 adsapi_php.ini 將檔案複製到主目錄中:

    composer require googleads/googleads-php-lib
    curl https://raw.githubusercontent.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\v202408\ApiException;
    use Google\AdsApi\AdManager\v202408\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 並建立新的專案 (Console 應用程式)。

  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 中設定憑證,請按照 參閱這篇維基文章 ,瞭解使用 AdManagerUser 類別的替代方式如要進一步瞭解如何使用 .NET 用戶端程式庫,請參閱 給 README 。如果您要在 .NET 不使用用戶端程式庫的情況下進行開發,請參閱 NoClientLibrary 維基文章

小茹

以下提供一個基本範例,示範如何將 Ruby 用戶端程式庫。Ruby 用戶端程式庫需要 Ruby 2.1 以上版本。

  1. 安裝 Ruby Gem 並取得設定檔。

    在殼層中執行下列指令:

    gem install google-dfp-api
    curl https://raw.githubusercontent.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, :v202408)
    
    # Make a request.
    network = network_service.get_current_network()
    
    puts "The current network is %s (%d)." %
            [network[:display_name], network[:network_code]]
    

如需詳細的入門步驟,請參閱 README 檔案。另外,您也可以參閱我們的完整 Ruby 適用的範例程式庫

後續步驟

當您建立並執行用戶端程式庫後,請修改提供的範例, 您可以視需求擴充這些內容

詳情請參閱參考說明文件 關於 API

如需協助,請前往支援頁面