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:
- 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.
- 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
, andexpires_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_tokenUse 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 anaccess_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,