How to request a refresh token for the Bing Ads API

Isis Oliveira 0 Reputation points
2024-04-29T13:34:45.8366667+00:00

I'm currently in the process of setting up a new connector on Airbyte for the Bing Ads API.

However, I've encountered a requirement where a refresh token is mandatory. Could someone please guide me on how to obtain this token? I have the necessary prerequisites, including a Microsoft Advertising Account and an Microsoft Developer Token. Additionally, I'm attempting to make the request using Python. Any assistance would be greatly appreciated. Thank you.

The documentation and examples are very confusing to me, but I don't think I need to register an application in Azure just to access this API.

Microsoft Advertising
Microsoft Advertising
A platform for Microsoft's advertising efforts designed to manage all advertising and reporting for partner advertisers. Previously known as Bing Ads and adCenter.
54 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Claudia Dos Santos Haz (CONCENTRIX CORPORATION) 775 Reputation points Microsoft Vendor
    2024-04-30T15:04:42.4166667+00:00

    Hi @Isis Oliveira,

    Thank you for reaching out to Microsoft Q&A forum!

    Certainly! To request a refresh token for the Bing Ads API, follow these steps:

    1. Register an Application:
      • Sign in with your Microsoft account credentials and grant your app consent to manage your Microsoft Advertising accounts.
      • After signing in, your browser should be redirected to this URL with a code in the address bar. You can ignore any error messages on the page.
    2. Redeem the Authorization Code:
      • Once the user has granted consent, you’ll receive an authorization code.
      • Use this code to request an access token by redeeming it.
      • Get the access_token, refresh_token, and expires_in values from the JSON response stream.
      • The expires_in value represents the maximum time in seconds until the access token expires.
      • Here’s an example of how to do this using PowerShell:
      • Sign in with your Microsoft account credentials and grant your app consent to manage your Microsoft Advertising accounts.
      • After signing in, your browser should be redirected to this URL with a code in the address bar. You can ignore any error messages on the page.

      Replace 'your_client_id' with your registered application ID.

      $clientId = "your_client_id"

      Open the consent URL in the browser.

      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"

      Enter the response URI after granting consent.

      $code = Read-Host "Enter the response URI here:"

      Extract the code from the response URI.

      $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://login.microsoftonline.com/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

      Use the refresh token to get new access and refresh tokens before the access token expires.

      $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) Once you have successfully acquired an access_token, you can use it in requests to Bing Ads APIs. Refer to the Make your first API call guide for an example.

    Remember to replace your_client_id with your actual application ID. Happy coding! 😊

    Best regards,

    0 comments No comments