Bing Ads API での Python の使用を開始する

Python を使用してBing Ads API アプリケーションの開発を開始するには、 提供されている例 から始めるか、 Web アプリケーションまたは デスクトップ アプリケーションのアプリケーション チュートリアルのいずれかに従います。

運用環境またはサンドボックス内の Microsoft Advertising へのアクセス権を持つユーザー資格情報が必要です。 運用環境では、 運用開発者トークンが必要です。 すべてのサンドボックス クライアントは、ユニバーサル サンドボックス開発者トークン ( BBD37VB98) を使用できます。 詳細については、「 Bing広告 APIサンドボックスの概要」を参照してください。

OAuth を使用して認証するには、アプリケーションを登録し、対応するクライアント識別子を取得する必要もあります。 また、Web アプリケーションを開発する場合は、クライアント シークレットとリダイレクト URI もメモする必要があります。 運用環境でのアプリケーションの登録と承認コード付与フローの詳細については、「 OAuth による認証 」と 「SDK による認証」を参照してください。 運用環境では独自のアプリケーション ID (クライアント ID) を使用する必要がありますが、すべての Microsoft Advertising のお客様は、サンドボックス内のパブリック "Tutorial Sample App" クライアント ID ( 4c0b021c-00c3-4508-838f-d3127e8167ff) を使用できます。 詳細については、「 サンドボックス」を参照してください。

依存関係

Bing Ads Python SDK では、すべてのBing Ads API プログラミング要素のプロキシとして 、suds-jurko-0.6 ライブラリが使用されます。 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 Ads Python SDK をインストールしたら、 GitHub からサンプルをダウンロードするか、または 「チュートリアル: Python のBing Ads API Web アプリケーション」または「チュートリアル: Bing Ads APIDesktop Application in Python」アプリケーションの アプリケーション チュートリアルのいずれかに従うことができます。

Suds の使用

Bing Ads Python SDK では 、suds-jurko-0.6 SOAP SDK を使用して、Bing Ads API のプログラミング要素 (サービス操作、データ オブジェクト、値セット) をインスタンス化します。 ServiceClientBulkServiceManager、または ReportingServiceManager クラスを使用して、Suds ファクトリ オブジェクトを渡します。 Suds は SDK の依存関係として含まれるため、Suds を直接使用して、Bing Ads API サービス操作のいずれかを呼び出すことができます。

Bing Ads Python SDK で、Suds に関連する次のルール、提案、ヒントに留意してください。

  • 最も一般的な例外の 1 つは 、ERROR:suds.resolver:(ClassGoesHere) が見つかりません。 通常、これは、たとえば、 Suds オブジェクトの名前空間プレフィックスを使用して解決できます。 ns3:ArrayOfstring

    ヒント

    各サービスで使用できる名前空間プレフィックスを持つすべての SOAP オブジェクトを検出するには、soap クライアントを印刷します。 たとえば、次のステートメントは、キャンペーン、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'
                },
            ]
        }
    
  • ExpandedTextAdNegativeKeyword、NegativeKeywordList などの派生型の場合、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)
    
  • 非プリミティブ要素は、たとえば、AdEditorialStatus 型の EditorialStatus など、Suds クライアントに対して指定する必要があります。ただし、Bing Ads API ではそのような要素は必要ありません。

  • Campaign Management API 操作では、非プリミティブを指定する場合は、サービスによって定義された値のいずれかである必要があります。つまり、nil 要素にすることはできません。 Suds は非プリミティブを必要とし、Microsoft Advertising は列挙値の代わりに nil 要素を受け入れないので、非プリミティブを設定するか、None に設定する必要があります。 また、要素の準備が整っている場合は、要素を None に設定する必要があることにも注意してください。 たとえば、 を設定 expanded_text_ad.EditorialStatus=Noneします。

Bing Ads API サービス操作の対応するメソッドを呼び出すには、 ServiceClient クラスのインスタンスを使用して、Suds ファクトリ オブジェクトを渡します。 詳細については、「 SDK を使用した認証」を参照してください。

関連項目

Bing Ads API クライアント ライブラリ
Bing Ads API のコード例
Bing Ads API Web サービス アドレス
サービス エラーと例外の処理
サンドボックス