Accessing the Bing Ads API in a Console Application

Although there’s plenty of documentation by Microsoft on how to use their Bing Ads API the documentation on how to initially connect to it seems a bit fragmented so this is an attempt to summarize all the steps required to grant a console application access to the API.

Most of these details come from these articles.

In order to connect to the API you need the following pieces of information.

  • Developer Token
  • Customer ID
  • Account ID
  • Client ID
  • Refresh Token
Developer Token

This can be found here.

Customer ID & Account ID

These can be found here, they’re present in the URL as the customer and account route values.

	
https://ui.bingads.microsoft.com/campaign/Campaigns.m?cid=<CUSTOMERID>&aid=<ACCOUNTID>#/customer/<CUSTOMERID>/account/<ACCOUNTID>/campaign
	
Client ID & Refresh Token

In my case I wanted a console application to be able to access the Bing Ads API and get details of ongoing campaigns without any user interaction so I wanted to get the initial refresh token manually and then allow the application to request a new one when required.

To allow the console application to authenticate it needs to be registered in the application portal as specified here. This application should be created as a “Native Application”, the Application ID for this application is the Client ID that is used to authenticate with OAuth and generate a Refresh token.

You should then use Client ID from the created application in the below URL, the client state code can be whatever you like.

	
https://login.live.com/oauth20_authorize.srf?client_id=<CLIENTID>&scope=bingads.manage&response_type=token&redirect_uri=https://login.live.com/oauth20_desktop.srf&state=<CLIENTSTATECODE>
	

This URL will take you to a login screen where you should login with the same account that is used to access the Bing Ads account, it will then ask for permission to access Bing Ads on your behalf.

Once accepted it will redirect to a blank webpage from which you can extract the access code from the URL.

	
https://login.live.com/oauth20_desktop.srf?code=<CODE>&state=<CLIENTSTATECODE>&lc=1033
	

The code and Client ID should then be posted to the OAuth login service to get the refresh token.

	
POST https://login.live.com/oauth20_token.srf HTTP/1.1
Accept: application/json
Content-Type: application/x-www-form-urlencoded
Host: login.live.com
Content-Length: ContentLengthGoesHere

client_id=<CLIENTID>&scope=bingads.manage&code=<CODE>&grant_type=authorization_code&redirect_uri=https%3A%2F%2Flogin.live.com%2Foauth20_desktop.srf
	

The returned refresh token can then be used in conjunction with your developer token and clientId in your console application, code for a sample application can be found here.


Leave a Reply

Your email address will not be published. Required fields are marked *