次の方法で共有


最初の API 呼び出しを行う

重要

2022 年 6 月、Bing広告の要件として 多要素認証 を導入しました。 この要件に準拠するには、引き続きコードの変更が必要な場合があります。 Microsoft Advertising は、10 月初旬に技術的な適用チェックを実行しています。

このブログ投稿 では、コンプライアンスを確保するために実行する必要がある手順について説明します。

詳細については、 多要素認証要件 ガイドを参照してください。

すぐに作業したい場合は、次の手順に従って Microsoft Advertising ユーザー情報を取得します。

運用クイック スタート

運用環境で認証するには、まず アプリケーションを登録する必要があります。 Microsoft アカウントの資格情報でサインインし、Microsoft Advertising アカウントの管理に アプリ の同意を付与します。

  1. 新しいファイルを作成し、次のスクリプトを貼り付けます。 $clientIdを登録済みアプリのアプリケーション ID に設定します。 Web アプリケーションをクライアント シークレットに登録した場合は、アクセス トークンを要求するときに $client_secret=YourWebAppClientSecret も含める必要があります。

注:

のyour_client_id を、 Azure portal - アプリ登録ポータル によってアプリが割り当てられたアプリケーション (クライアント) ID に置き換えます。

# Replace your_client_id with your registered application ID. 
$clientId = "your_client_id"

Start-Process "https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=$clientId&scope=openid%20profile%20https://ads.microsoft.com/msads.manage%20offline_access&response_type=code&redirect_uri=https://login.microsoftonline.com/common/oauth2/nativeclient&state=ClientStateGoesHere&prompt=login"

$code = Read-Host "Grant consent in the browser, and then enter the response URI here:"
$code = $code -match 'code=(.*)\&'
$code = $Matches[1]

# Get the initial access and refresh tokens. 

$response = Invoke-WebRequest https://login.microsoftonline.com/common/oauth2/v2.0/token -ContentType application/x-www-form-urlencoded -Method POST -Body "client_id=$clientId&scope=https://ads.microsoft.com/msads.manage%20offline_access&code=$code&grant_type=authorization_code&redirect_uri=https%3A%2F%2Flogin.microsoftonline.com%2Fcommon%2Foauth2%2Fnativeclient"

$oauthTokens = ($response.Content | ConvertFrom-Json)  
Write-Output "Access token: " $oauthTokens.access_token  
Write-Output "Access token expires in: " $oauthTokens.expires_in  
Write-Output "Refresh token: " $oauthTokens.refresh_token 

# The access token will expire e.g., after one hour. 
# Use the refresh token to get new access and refresh tokens. 

$response = Invoke-WebRequest https://login.microsoftonline.com/common/oauth2/v2.0/token -ContentType application/x-www-form-urlencoded -Method POST -Body "client_id=$clientId&scope=https://ads.microsoft.com/msads.manage%20offline_access&code=$code&grant_type=refresh_token&refresh_token=$($oauthTokens.refresh_token)"

$oauthTokens = ($response.Content | ConvertFrom-Json)  
Write-Output "Access token: " $oauthTokens.access_token  
Write-Output "Access token expires in: " $oauthTokens.expires_in  
Write-Output "Refresh token: " $oauthTokens.refresh_token

ファイルを保存して Get-Tokens-Production.ps1 名前を付けます (名前は任意ですが、拡張子は .ps1 する必要があります)。

Microsoft Advertising アカウントをプログラムで管理するには、Web アプリケーションの同意フローを通じて少なくとも 1 回は同意を提供する必要があります。 それ以降は、最新の更新トークンを使用して、ユーザーの操作を行わずに新しいアクセストークンと更新トークンを要求できます。

  1. 次に、コンソール ウィンドウ Get-Tokens-Production.ps1 開きます。 コマンド プロンプトで、 Get-Tokens-Production.ps1 保存したフォルダーに移動し、次のコマンドを入力します。

    powershell.exe -File .\Get-Tokens-Production.ps1
    

    PowerShell スクリプトが正常に実行されると、Microsoft Advertising 資格情報を入力するブラウザー セッションが開始されます。 同意すると、ブラウザーのアドレス バーに許可コードが含まれます (「?code=UseThisCode&...」を参照してください)。

    https://login.microsoftonline.com/common/oauth2/nativeclient?code=M.R4_BAY.f202904c-2269-4daf-1e21-862ed4d49143
    

    許可コード (独自のコードではなく、例 M.R4_BAY.f202904c-2269-4daf-1e21-862ed4d49143) をコピーし、プロンプトのコンソール ウィンドウに入力します。 PowerShell スクリプトは、アクセス トークンと更新トークンを返します。 (スクリプトは、トークンを更新する方法の例として、Invoke-WebRequest に対して 2 回目の呼び出しを行います)。更新トークンは、パスワードと同様に扱う必要があります。誰かがそれを手に入れると、リソースにアクセスできます。 更新トークンは有効期間が長いですが、無効になる可能性があります。 invalid_grant エラーが発生した場合、更新トークンは無効になり、ユーザーの同意と新しい更新トークンを取得するには、 Get-Tokens-Production.ps1 PowerShell スクリプトをもう一度実行する必要があります。

  2. 新しいファイルを作成し、次のスクリプトを貼り付けます。 accessTokenGet-Tokens-Production.ps1から受け取った値に設定し、ここで説明する手順に従って受け取った開発者トークンに$developerTokenを設定します。

    $accessToken = "AccessTokenGoesHere";
    $developerToken = "DeveloperTokenGoesHere";
    
    [xml]$getUserRequest = 
    '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v13="https://bingads.microsoft.com/Customer/v13">
    <soapenv:Header>
       <v13:DeveloperToken>{0}</v13:DeveloperToken>
       <v13:AuthenticationToken>{1}</v13:AuthenticationToken>
    </soapenv:Header>
    <soapenv:Body>
       <v13:GetUserRequest>
          <v13:UserId xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
       </v13:GetUserRequest>
    </soapenv:Body>
    </soapenv:Envelope>' -f $developerToken, $accessToken
    
    $headers = @{"SOAPAction" = "GetUser"}
    
    $uri = "https://clientcenter.api.bingads.microsoft.com/Api/CustomerManagement/v13/CustomerManagementService.svc"
    $response = Invoke-WebRequest $uri -Method post -ContentType 'text/xml' -Body $getUserRequest -Headers $headers
    Write-Output $response.Content
    

    ファイルを保存して Get-User.ps1 名前を付けます (名前は任意ですが、拡張子は .ps1 する必要があります)。

  3. 次に、コンソール ウィンドウ Get-User.ps1 開きます。 コマンド プロンプトで、 Get-User.ps1 保存したフォルダーに移動し、次のコマンドを入力します。

    powershell.exe -File .\Get-User.ps1
    

    PowerShell スクリプトが正常に実行されると、顧客ロールを含む Microsoft Advertising ユーザーの詳細が出力されます。 詳細については、「 GetUser」を参照してください。

サンドボックス クイック スタート

サンドボックス環境で認証するには、アプリケーションを登録する必要はありません。 パブリックの "チュートリアル サンプル アプリ" クライアント ID ( 4c0b021c-00c3-4508-838f-d3127e8167ff) を使用するだけです。

  1. Microsoft Advertising サンドボックス アカウントにサインアップします。 Microsoft アカウント (MSA) のメール アドレスは outlook-int.com ( someone@outlook-int.com など) である必要があります。 詳細については、「 サンドボックス」を参照してください。

  2. 新しいファイルを作成し、次のスクリプトを貼り付けます。

    # Replace the Tutorial Sample App ID with your registered application ID. 
    $clientId = "4c0b021c-00c3-4508-838f-d3127e8167ff"
    
    Start-Process "https://login.windows-ppe.net/consumers/oauth2/v2.0/authorize?client_id=$clientId&scope=openid%20profile%20https://api.ads.microsoft.com/msads.manage%20offline_access&response_type=code&redirect_uri=https://login.windows-ppe.net/common/oauth2/nativeclient&state=ClientStateGoesHere&prompt=login"
    
    $code = Read-Host "Grant consent in the browser, and then enter the response URI here:"
    $code = $code -match 'code=(.*)\&'
    $code = $Matches[1]
    
    # Get the initial access and refresh tokens. 
    
    $response = Invoke-WebRequest https://login.windows-ppe.net/consumers/oauth2/v2.0/token -ContentType application/x-www-form-urlencoded -Method POST -Body "client_id=$clientId&scope=https://api.ads.microsoft.com/msads.manage%20offline_access&code=$code&grant_type=authorization_code&redirect_uri=https://login.windows-ppe.net/common/oauth2/nativeclient"
    
    $oauthTokens = ($response.Content | ConvertFrom-Json)  
    Write-Output "Access token: " $oauthTokens.access_token  
    Write-Output "Access token expires in: " $oauthTokens.expires_in  
    Write-Output "Refresh token: " $oauthTokens.refresh_token 
    
    # The access token will expire e.g., after one hour. 
    # Use the refresh token to get new access and refresh tokens. 
    
    $response = Invoke-WebRequest https://login.windows-ppe.net/consumers/oauth2/v2.0/token -ContentType application/x-www-form-urlencoded -Method POST -Body "client_id=$clientId&scope=https://api.ads.microsoft.com/msads.manage%20offline_access&code=$code&grant_type=refresh_token&refresh_token=$($oauthTokens.refresh_token)"
    
    $oauthTokens = ($response.Content | ConvertFrom-Json)  
    Write-Output "Access token: " $oauthTokens.access_token  
    Write-Output "Access token expires in: " $oauthTokens.expires_in  
    Write-Output "Refresh token: " $oauthTokens.refresh_token 
    

    ファイルを保存して Get-Tokens-Sandbox.ps1 名前を付けます (名前は任意ですが、拡張子は .ps1 する必要があります)。

    ユーザーは、Web アプリケーションの同意フローを通じて少なくとも 1 回は同意を提供する必要があります。 それ以降は、最新の更新トークンを使用して、ユーザーの操作を行わずに新しいアクセストークンと更新トークンを要求できます。

  3. 次に、コンソール ウィンドウ Get-Tokens-Sandbox.ps1 開きます。 コマンド プロンプトで、 Get-Tokens-Sandbox.ps1 保存したフォルダーに移動し、次のコマンドを入力します。

    powershell.exe -File .\Get-Tokens-Sandbox.ps1
    

    PowerShell スクリプトが正常に実行されると、Microsoft Advertising 資格情報を入力するブラウザー セッションが開始されます。 同意すると、ブラウザーのアドレス バーに許可コードが含まれます (「?code=UseThisCode&...」を参照してください)。

    https://login.windows-ppe.net/common/oauth2/nativeclient?code=M.R0_CD1.132de532-5105-7550-b1fd-d37f9af2f009
    

    許可コード (独自のコードではなく、例 M.R0_CD1.132de532-5105-7550-b1fd-d37f9af2f009) をコピーし、プロンプトのコンソール ウィンドウに入力します。 PowerShell スクリプトは、アクセス トークンと更新トークンを返します。 (スクリプトは、トークンを更新する方法の例として、Invoke-WebRequest に対して 2 回目の呼び出しを行います)。更新トークンは、パスワードと同様に扱う必要があります。誰かがそれを手に入れると、リソースにアクセスできます。 更新トークンは有効期間が長いですが、無効になる可能性があります。 invalid_grant エラーが発生した場合、更新トークンは無効になり、ユーザーの同意と新しい更新トークンを取得するには、 Get-Tokens-Sandbox.ps1 PowerShell スクリプトをもう一度実行する必要があります。

  4. 新しいファイルを作成し、次のスクリプトを貼り付けます。 accessTokenを、Get-Tokens-Sandbox.ps1から受け取った値に設定します。

    $accessToken = "AccessTokenGoesHere";
    $developerToken = "BBD37VB98";
    
    [xml]$getUserRequest = 
    '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v13="https://bingads.microsoft.com/Customer/v13">
    <soapenv:Header>
       <v13:DeveloperToken>{0}</v13:DeveloperToken>
       <v13:AuthenticationToken>{1}</v13:AuthenticationToken>
    </soapenv:Header>
    <soapenv:Body>
       <v13:GetUserRequest>
          <v13:UserId xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
       </v13:GetUserRequest>
    </soapenv:Body>
    </soapenv:Envelope>' -f $developerToken, $accessToken
    
    $headers = @{"SOAPAction" = "GetUser"}
    
    $uri = "https://clientcenter.api.sandbox.bingads.microsoft.com/Api/CustomerManagement/v13/CustomerManagementService.svc"
    $response = Invoke-WebRequest $uri -Method post -ContentType 'text/xml' -Body $getUserRequest -Headers $headers
    Write-Output $response.Content
    

    ファイルを保存して Get-User.ps1 名前を付けます (名前は任意ですが、拡張子は .ps1 する必要があります)。

  5. 次に、コンソール ウィンドウ Get-User.ps1 開きます。 コマンド プロンプトで、 Get-User.ps1 保存したフォルダーに移動し、次のコマンドを入力します。

    powershell.exe -File .\Get-User.ps1
    

    PowerShell スクリプトが正常に実行されると、顧客ロールを含む Microsoft Advertising ユーザーの詳細が出力されます。 詳細については、「 GetUser」を参照してください。

関連項目

概要