共用方式為


開始搭配 Bing 廣告 API 使用 Python

若要開始使用 Python 開發 Bing 廣告 API 應用程式,您可以從 提供的範例 開始,或遵循 Web傳統型 應用程式的其中一個應用程式逐步解說。

您需要可在 生產沙箱中存取 Microsoft Advertising 的使用者認證。 針對生產環境,您需要 生產開發人員權杖。 所有沙箱用戶端都可以使用通用沙箱開發人員權杖,也就是 BBD37VB98。 如需詳細資訊,請 參閱開始使用 Bing 廣告 API沙箱

若要透過 OAuth 進行驗證,您也必須註冊應用程式並取得對應的用戶端識別碼。 如果您要開發 Web 應用程式,也需要記下用戶端密碼和重新導向 URI。 如需在生產環境中註冊應用程式和授權碼授與流程的詳細資訊,請參閱 使用 OAuth 進行驗證 和使用 SDK 進行驗證。 雖然在生產環境中,您必須使用自己的應用程式識別碼 (也就是用戶端識別碼) ,但所有 Microsoft Advertising 客戶都可以在沙箱中使用公用的「教學課程範例應用程式」用戶端識別碼,也就是 4c0b021c-00c3-4508-838f-d3127e8167ff。 如需詳細資訊,請參閱 沙箱

相依性

Bing 廣告 Python SDK 會使用 suds-jurko-0.6 程式庫作為所有 Bing 廣告 API 程式設計項目的 Proxy。 如需搭配 Microsoft Advertising 使用 Suds 的詳細資訊,請參閱 使用 Suds

Bing Ads Python SDK 支援 Python 3.3 或更新版本。 您應該安裝並執行其中一個支援的版本。

安裝 SDK

若要第一次安裝 Bing Ads Python SDK ,請從您的 IDE 或命令列提示字元執行下列命令。

pip.exe install bingads

若要確認已安裝 Bing Ads Python SDK,請執行下列命令。 您應該會在輸出清單 中看到 bingads (< 版本 >)

pip.exe list

如果您已經安裝 Bing Ads Python SDK,您可以執行此命令來取得最新的位。

pip.exe install --upgrade bingads

逐步解說

安裝 Bing 廣告 Python SDK 之後,您可以從 GitHub 下載範例,或遵循逐步解說的其中一個應用程式逐步 解說:Python 中的 Bing 廣告 API Web 應用程式逐步解說:Python 應用程式中的 Bing 廣告 API 桌面 應用程式。

使用 Suds

Bing Ads Python SDK 會使用 suds-jurko-0.6 SOAP SDK 來具現化 Bing 廣告 API 的程式設計項目,例如服務作業、資料物件和值集。 您將透過ServiceClientBulkServiceManager 或 ReportingServiceManager類別傳遞 Suds Factory 物件。 由於 Suds 包含為 SDK 相依性,因此您可以直接使用 Suds 來呼叫任何 Bing 廣告 API 服務作業。

請記住 Bing Ads Python SDK 中與 Suds 相關的下列規則、建議和秘訣。

  • 我們聽到的最常見例外狀況之一是 ERROR:suds.resolver: (ClassGoesHere) 找不到。 通常可以使用 Suds 物件的命名空間前置詞來解決此問題,例如 。 ns3:ArrayOfstring

    提示

    若要探索具有每個服務可用之命名空間前置詞的所有 SOAP 物件,您可以列印 soap 用戶端。 例如,下列語句會傳回 Campaign、AdGroup、ExpandedTextAd 和 Keyword 等等。

    campaign_service = ServiceClient(
        service='CampaignManagementService', 
        version = 13,
        authorization_data=authorization_data, 
        environment = ENVIRONMENT,
    )
    print campaign_service.soap_client
    
  • 對於透過 Suds 傳遞至行銷活動管理服務的許多物件,您可以建立字典物件。 從效能觀點來看,字典方法比替代 service.factory.create 方法更快。

    ad_groups = {
        'AdGroup':
            [
                {
                    'Name': "Women's Shoe Sale",
                    'AdDistribution': 'Search',
                    'EndDate': {
                        'Day': '31',
                        'Month': '12',
                        'Year': strftime("%Y", gmtime())
                    },
                    'CpcBid': {
                        'Amount': 0.09
                    },
                    'Language': 'English'
                },
            ]
        }
    
  • 若為 ExpandedTextAdNegativeKeywordNegativeKeywordList等衍生類型,Suds 程式庫會要求您使用 factory.create。

    ads = campaign_service.factory.create('ArrayOfAd')
    expanded_text_ad=campaign_service.factory.create('ExpandedTextAd')
    expanded_text_ad.TitlePart1='Contoso'
    expanded_text_ad.TitlePart2='Quick & Easy Setup'
    expanded_text_ad.TitlePart3='Seemless Integration'
    expanded_text_ad.Text='Find New Customers & Increase Sales!'
    expanded_text_ad.TextPart2='Start Advertising on Contoso Today.'
    expanded_text_ad.Path1='seattle'
    expanded_text_ad.Path2='shoe sale'
    expanded_text_ad.Type='ExpandedText'
    expanded_text_ad.Status=None
    expanded_text_ad.EditorialStatus=None
    
    # With FinalUrls you can separate the tracking template, custom parameters, and 
    # landing page URLs.
    final_urls=campaign_service.factory.create('ns3:ArrayOfstring')
    final_urls.string.append('https://www.contoso.com/womenshoesale')
    expanded_text_ad.FinalUrls=final_urls
    
    # Final Mobile URLs can also be used if you want to direct the user to a different page 
    # for mobile devices.
    final_mobile_urls=campaign_service.factory.create('ns3:ArrayOfstring')
    final_mobile_urls.string.append('https://mobile.contoso.com/womenshoesale')
    expanded_text_ad.FinalMobileUrls=final_mobile_urls
    
    # Set custom parameters that are specific to this ad.
    url_custom_parameters=campaign_service.factory.create('CustomParameters')
    parameters=campaign_service.factory.create('ArrayOfCustomParameter')
    custom_parameter1=campaign_service.factory.create('CustomParameter')
    custom_parameter1.Key='promoCode'
    custom_parameter1.Value='PROMO' + str(index)
    parameters.CustomParameter.append(custom_parameter1)
    custom_parameter2=campaign_service.factory.create('CustomParameter')
    custom_parameter2.Key='season'
    custom_parameter2.Value='summer'
    parameters.CustomParameter.append(custom_parameter2)
    url_custom_parameters.Parameters=parameters
    expanded_text_ad.UrlCustomParameters=url_custom_parameters
    
    ads.Ad.append(expanded_text_ad)
    
  • 必須為 Suds 用戶端指定任何非基本元素,例如AdEditorialStatus類型的編輯狀態,即使 Bing 廣告 API 不需要這類元素也一樣。

  • 行銷活動管理 API 作業需要,如果您指定非基本類型,它必須是服務所定義的其中一個值,也就是它不能是 nil 元素。 由於 Suds 需要非基本類型,且 Microsoft Advertising 不會接受 nil 元素來取代列舉值,因此您必須設定非基本類型,否則必須設定為 None。 另請注意,如果元素已就緒,您必須將它設定為 None。 例如,設定 expanded_text_ad.EditorialStatus=None

若要呼叫 Bing Ads API 服務作業的對應方法,您可以使用 ServiceClient 類別的實例,並傳遞 Suds Factory 物件。 如需詳細資訊,請 參閱使用 SDK 進行驗證

另請參閱

Bing 廣告 API 用戶端程式庫
Bing 廣告 API 程式碼範例
Bing 廣告 API Web 服務位址
處理服務錯誤和例外狀況
沙 箱