进行第一次 API 调用

重要

2022 年 6 月,我们引入了 多重身份验证 作为Bing Ads的要求。 可能需要更改代码才能符合此要求。 Microsoft广告公司正在 10 月初进行技术强制检查。

此博客文章 概述了确保合规性应采取的步骤。

有关详细信息,请参阅 多重身份验证要求 指南。

如果只想立即获得一些内容,请按照以下步骤获取Microsoft广告用户信息。

生产快速入门

若要在生产环境中进行身份验证,应首先 注册应用程序。 使用Microsoft帐户凭据登录,并授予 应用 管理Microsoft广告帐户的许可。

  1. 创建一个新文件并将以下脚本粘贴到其中。 设置为 $clientId 已注册应用的应用程序 ID。 如果使用客户端密码注册了 Web 应用程序,则在请求访问令牌时还需要包含 $client_secret=YourWebAppClientSecret

注意

将下面的your_client_id替换为Azure 门户 -应用注册门户分配应用的应用程序 (客户端) 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广告帐户,必须至少通过 Web 应用程序同意流提供一次同意。 此后,可以使用最新的刷新令牌来请求新的访问和刷新令牌,而无需进行任何进一步的用户交互。

  1. 现在,若要运行 Get-Tokens-Production.ps1 ,请打开控制台窗口。 在命令提示符下,导航到保存 Get-Tokens-Production.ps1 的文件夹并输入以下命令:

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

    PowerShell 脚本成功运行时,它会启动浏览器会话,你可以在其中输入Microsoft广告凭据。 同意后,浏览器的地址栏包含授予代码 (请参阅 ?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 进行第二次调用,作为如何刷新令牌的示例。) 应将刷新令牌视为密码;如果有人掌握了它,他们有权访问你的资源。 刷新令牌的生存期很长,但可能会变得无效。 如果收到invalid_grant错误,则刷新令牌不再有效,需要再次运行 Get-Tokens-Production.ps1 PowerShell 脚本以获取用户同意和新的刷新令牌。

  2. 创建一个新文件并将以下脚本粘贴到其中。 accessToken按照此处所述的步骤,将 设置为从Get-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广告用户的详细信息,包括客户角色。 有关详细信息,请参阅 GetUser

沙盒快速入门

若要在沙盒环境中进行身份验证,无需注册应用程序。 只需使用公共“教程示例应用”客户端 ID,即 00001111-aaaa-2222-bbbb-3333cccc4444

  1. 注册 Microsoft广告 沙盒帐户。 Microsoft帐户 (MSA) 电子邮件地址必须是 outlook-int.com (例如, someone@outlook-int.com) 。 有关详细信息,请参阅 沙盒

  2. 创建一个新文件并将以下脚本粘贴到其中。

    # Replace the Tutorial Sample App ID with your registered application ID. 
    $clientId = "00001111-aaaa-2222-bbbb-3333cccc4444"
    
    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 应用程序同意流提供一次同意。 此后,可以使用最新的刷新令牌来请求新的访问和刷新令牌,而无需进行任何进一步的用户交互。

  3. 现在,若要运行 Get-Tokens-Sandbox.ps1 ,请打开控制台窗口。 在命令提示符下,导航到保存 Get-Tokens-Sandbox.ps1 的文件夹并输入以下命令:

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

    PowerShell 脚本成功运行时,它会启动浏览器会话,你可以在其中输入Microsoft广告凭据。 同意后,浏览器的地址栏包含授予代码 (请参阅 ?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 进行第二次调用,作为如何刷新令牌的示例。) 应将刷新令牌视为密码;如果有人掌握了它,他们有权访问你的资源。 刷新令牌的生存期很长,但可能会变得无效。 如果收到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广告用户的详细信息,包括客户角色。 有关详细信息,请参阅 GetUser

另请参阅

入门