語言

開始使用 Bing Ads API

任何擁有開發人員權杖的 Microsoft Advertising 使用者都可以開始使用 Bing Ads API。 對於放置大量廣告的廣告客戶或建置廣告工具的開發人員,Bing Ads API 提供了對 Microsoft Advertising 的程式設計介面。

您可以使用支援 Web 服務的任何語言開發 Bing Ads API 應用程式。 若要開始使用特定 SDK,請參閱開始使用 C# | Java | 、PHP | Python。

取得使用者存取權杖

考慮您要登入的使用者,例如 example@contoso.com。 Bing Ads API 將不接受該電子郵件地址和密碼。 您必須改為設定包含使用者存取權杖的 AuthenticationToken 標頭元素。 您可以將存取權杖視為代表使用者名稱和密碼。

如何取得使用者的存取權杖? 身為應用程式開發人員,您會使用 Microsoft 授權 URL 來提示 Microsoft Advertising 使用者同意。 一旦使用者提供同意,您就可以取得存取權杖並代表使用者採取行動。 存取權杖代表具有一或多個 Microsoft Advertising 帳戶存取權的使用者認證。

  1. 註冊應用程式

  2. 要求使用者同意 您的應用程式管理其 Microsoft Advertising 帳戶

  3. 取得存取權和重新整理權杖

  4. 進行您的第一個 API 呼叫

提示

如需如何使用 Bing Ads SDK 取得存取權和重新整理權杖的詳細資訊,請參閱使用 SDK 驗證。

取得開發人員權杖

若要使用 Bing Ads API,您必須擁有開發人員權杖和有效的使用者認證。 如果您還沒有 Microsoft Advertising 帳戶,可以透過 Microsoft Advertising 網頁應用程式註冊。

注意事項

沙箱和生產環境使用不同的認證。 您可以在此處註冊沙箱帳戶。 每個人都可以使用通用沙盒開發者代幣,即 BBD37VB98。

您可以遵循下列步驟,取得用於生產的開發人員權杖。

注意事項

自 2025 年 5 月 31 日起,開發人員入口網站頁面將被取代,並以這裡 的新版本取代。 請更新您的書籤並開始使用新頁面以避免任何中斷。 如有問題或協助,請連絡 支援人員。

  1. 在 Microsoft Advertising 開發人員入口網站帳戶索引標籤中使用超級管理員認證登入。
  2. 選擇您要與開發人員權杖相關聯的使用者。 一般而言,應用程式只需要一個通用權杖,而不論支援多少使用者。
  3. 按一下 [要求權杖 ] 按鈕。

通用開發人員權杖可用於向任何 Microsoft Advertising 使用者認證進行驗證。 無論您的應用程式將由一或多個 Microsoft Advertising 使用者使用,您都可以使用相同的通用開發人員權杖。 自 2019 年 7 月開始,這是預設的權杖類型。

單一使用者開發人員權杖只能用來驗證一個使用者,以便存取一個客戶。 此 Token 類型已被取代,取而代之的是通用 Token。 如果您仍然看到單一使用者權杖已指派給其中一個使用者,您可以選取 [升級至通用]。

開發人員權杖可讓您以程式設計方式存取使用者允許的帳戶。 取得 API 存取的開發人員權杖不會授與任何 Microsoft Advertising 帳戶額外的權限。 每個 Microsoft Advertising 使用者都會為他們可以存取的每個客戶指派一個角色,例如超級管理員或廣告客戶廣告活動管理員。 有了開發人員權杖,使用者可以透過 API 以程式設計方式使用 Microsoft Advertising Web 應用程式中可用的相同帳戶。

API 認證的使用位置

當您呼叫 GetCampaignsByAccountId 等服務操作時,必須指定 請求標頭 元素,例如 DeveloperToken、CustomerId 和 CustomerAccountId。

<s:Envelope xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Header xmlns="https://bingads.microsoft.com/CampaignManagement/v13">
    <Action mustUnderstand="1">GetCampaignsByAccountId</Action>
    <ApplicationToken i:nil="false">ValueHere</ApplicationToken>
    <AuthenticationToken i:nil="false">ValueHere</AuthenticationToken>
    <CustomerAccountId i:nil="false">ValueHere</CustomerAccountId>
    <CustomerId i:nil="false">ValueHere</CustomerId>
    <DeveloperToken i:nil="false">ValueHere</DeveloperToken>
  </s:Header>
  <s:Body>
    <GetCampaignsByAccountIdRequest xmlns="https://bingads.microsoft.com/CampaignManagement/v13">
      <AccountId>ValueHere</AccountId>
      <CampaignType>ValueHere</CampaignType>
    </GetCampaignsByAccountIdRequest>
  </s:Body>
</s:Envelope>

如果您使用其中一個 Microsoft Advertising SDK,則會使用 AuthorizationData 設定要求標頭元素。 如需有關 SDK 驗證程式庫的詳細資訊,請參閱使用 SDK 驗證。

var authorizationData = new AuthorizationData
{
    Authentication = <AuthenticationGoesHere>, 
    CustomerId = <CustomerIdGoesHere>,
    AccountId = <AccountIdGoesHere>,
    DeveloperToken = "<DeveloperTokenGoesHere>"
};
static AuthorizationData authorizationData = new AuthorizationData();
authorizationData.setAuthentication(<AuthenticationGoesHere>);
authorizationData.setCustomerId("<CustomerIdGoesHere>");
authorizationData.setAccountId("<AccountIdGoesHere>");
authorizationData.setDeveloperToken("<DeveloperTokenGoesHere>");
$authorizationData = (new AuthorizationData())
    ->withAuthentication($AuthenticationGoesHere)
    ->withCustomerId($CustomerIdGoesHere)
    ->withAccountId($AccountIdGoesHere)
    ->withDeveloperToken($DeveloperTokenGoesHere);
authorization_data = AuthorizationData(
    authentication = <AuthenticationGoesHere>,
    customer_id = <CustomerIdGoesHere>,
    account_id = <AccountIdGoesHere>,
    developer_token = '<DeveloperTokenGoesHere>'
)

取得您的帳戶和客戶識別碼

若要取得使用者的客戶識別碼和帳戶識別碼,您可以登入 Microsoft Advertising Web 應用程式,然後按一下 [廣告活動 ] 索引標籤。URL 的查詢字串中會包含識別客戶識別碼的 cid 機碼/值組,以及識別您帳戶識別碼的 協助 機碼/值組。 例如, https://ui.ads.microsoft.com/campaign/Campaigns.m?cid=FindCustomerIdHere&aid=FindAccountIdHere#/customer/FindCustomerIdHere/account/FindAccountIdHere/campaign。

提示

請勿將帳號誤認為帳戶識別碼。 帳號是系統產生的帳號,用來在 Microsoft Advertising Web 應用程式中識別帳戶。 帳號的格式為 xxxxxxxx,其中 xxxxxxxx 是一連串任意八個英數字元。 API 服務要求只會使用帳戶識別碼,絕不使用帳戶號碼。

使用客戶管理 API,您可以取得每個已驗證使用者的客戶和帳戶識別碼。

使用您的 Microsoft Advertising 認證和 DeveloperToken 呼叫 GetUser 。 在本文中,將 UserId nil 設定。 回應將包含包含 UserId 的 User 物件。

<?xml version="1.0" encoding="utf-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Header>
    <h:ApplicationToken i:nil="true" xmlns:h="https://bingads.microsoft.com/Customer/v13" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" />
    <h:AuthenticationToken xmlns:h="https://bingads.microsoft.com/Customer/v13">OAuthAccessTokenGoesHere</h:AuthenticationToken>
    <h:DeveloperToken xmlns:h="https://bingads.microsoft.com/Customer/v13">DeveloperTokenGoesHere</h:DeveloperToken>
  </s:Header>
  <s:Body>
    <GetUserRequest xmlns="https://bingads.microsoft.com/Customer/v13">
      <UserId i:nil="true" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" />
    </GetUserRequest>
  </s:Body>
</s:Envelope>

然後使用上一個步驟傳回的 UserId 呼叫 SearchAccounts 。 退回的廣告客戶帳戶 (或帳戶) 將包含帳戶和客戶識別碼。

<?xml version="1.0" encoding="utf-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Header>
    <h:ApplicationToken i:nil="true" xmlns:h="https://bingads.microsoft.com/Customer/v13" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" />
    <h:AuthenticationToken xmlns:h="https://bingads.microsoft.com/Customer/v13">OAuthAccessTokenGoesHere</h:AuthenticationToken>
    <h:DeveloperToken xmlns:h="https://bingads.microsoft.com/Customer/v13">DeveloperTokenGoesHere</h:DeveloperToken>
  </s:Header>
  <s:Body>
    <SearchAccountsRequest xmlns="https://bingads.microsoft.com/Customer/v13">
      <Predicates xmlns:a="https://bingads.microsoft.com/Customer/v13/Entities" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
        <a:Predicate>
          <a:Field>UserId</a:Field>
          <a:Operator>Equals</a:Operator>
          <a:Value>UserIdGoesHere</a:Value>
        </a:Predicate>
      </Predicates>
      <Ordering i:nil="true" xmlns:a="https://bingads.microsoft.com/Customer/v13/Entities" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" />
      <PageInfo xmlns:a="https://bingads.microsoft.com/Customer/v13/Entities" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
        <a:Index>0</a:Index>
        <a:Size>10</a:Size>
      </PageInfo>
    </SearchAccountsRequest>
  </s:Body>
</s:Envelope>

提示

如需傳回目前已驗證使用者帳戶的程式碼範例,請參閱 搜尋使用者帳戶程式碼範例 。

標頭元素參考

Bing Ads API 服務作業會使用簡單物件存取通訊協定 (SOAP) ,將要求和回應訊息與服務作業交換。 如需詳細資訊,請參閱 Bing 廣告 API 服務通訊協定。

每個 SOAP 要求都必須包含下列 SOAP 標頭,其中包含使用者的認證。

注意事項

CustomerAccountId 和 CustomerId 元素不適用於客戶計費和客戶管理服務。

元素 描述 資料類型
ApplicationToken 系統不會使用此標頭元素,應該忽略。 string
AuthenticationToken OAuth 存取權杖,代表具有 Microsoft Advertising 帳戶權限的 Microsoft 帳戶使用者。 如需詳細資訊,請參閱 使用 OAuth 驗證。 string
CustomerAccountId 擁有要求中實體的帳戶識別碼。 若兩者皆為必要,則此標頭元素必須與 AccountId body 元素具有相同的值。 此元素是多數服務作業的必要項目,最佳作法應一律設定它。 string
CustomerId 包含並擁有帳戶之客戶的識別碼。 如果您管理其他客戶的帳戶,則應使用該客戶 ID,而非您自己的客戶 ID。 此元素是多數服務作業的必要項目,最佳作法應一律設定它。 string
DeveloperToken 用來存取 Bing Ads API 的開發人員權杖。 string

需要協助嗎?

如需疑難排解提示,請參閱 處理服務錯誤和例外狀況。

Microsoft Q&A 論壇可供開發人員社群詢問和回答有關 Bing Ads API 和Microsoft廣告指令碼的問題。 Microsoft 會監視論壇,並回覆社群尚未回答的問題。

重要事項

若要確保我們看到您的問題,請使用 'advertising-api' 標記該問題。

如果調查涉及敏感的帳戶或個人詳細資料,或者您無法透過 Microsoft Q&A 找到解決問題所需的資訊,請連絡 Microsoft Advertising 支援。 若要有效解決問題,請提供支援,並提供參與 式支援中要求的詳細資料。

另請參閱

Microsoft Advertising API 概觀
Microsoft Advertising API 概念