Bing Search HTTP request with JSON result example?

dokondr picture dokondr · Oct 8, 2012 · Viewed 11.5k times · Source

Please help to figure out the parameters for Bing Search request that returns results in JSON.
Document "Migrating Bing Search API Applications" tells us:
To authenticate a Bing Search API request with Windows Azure Marketplace, you must obtain an account key. This mode of authentication replaces the AppID used in the Bing Search API 2.0.

On the other hand the same document provides the following example that still uses Appid:

http://api.search.live.net/xml.aspx?Appid=App&query=odata&sources=web&count=2

The following request:

curl "https://api.datamarket.azure.com/Data.ashx/Bing/SearchWeb/v1/Web?Query=%27xbox%27&$top=50&$format=json$accountKey=TPP....VRTWiq4=$Appid=conceptor"

Results in the following error:
The authorization type you provided is not supported. Only Basic and OAuth are supported

Please give an example of Bing search URL that can be used in CURL command line to get search results in JSON format.

Answer

user2453026 picture user2453026 · Jul 25, 2013

You need to send your app key as a Base64 encoded string in the basic auth header.

Authorization: Basic {{ encoded_app_key }}

BTW The previous answer links to an overview of the old Bing api, hence not useful if you are on the data marketplace api.

Here is your example using jQuery.

$.ajax({
    type:'POST',
    url:url,
    headers: {
        "Authorization": "Basic " + encodedAppKey
    }
}).done(function(data) { 
    alert(data);
});

Useful link: http://social.msdn.microsoft.com/Forums/windowsazure/en-US/9f085915-81b6-488d-a348-1c3ca769d44f/migrating-to-windows-azure-bing-search-api-with-jquery-jsonp