How do i return JSON results from BING Search Engine API
Solution 1
You need to remove the v1
from your URL, and add $format=json
to the end of your query, as detailed in the schema guide:
https://api.datamarket.azure.com/Bing/Search/Web?Query=%27Xbox%27&$format=json
To get the link to work, you need to provide it your hashed credentials, to get that, follow these steps:
- Login to the Azure Marketplace.
- Browse to the search api listing
- Click on the "Explore this Dataset" link (its only visible if you are logged in).
- Once it opens, on the top click on "Show" next to "Primary Account Key"
- Save this hashed value and use it in your code as the authentication digest.
Solution 2
Unfortunately Azure DataMarket and its APIs has been closed so you will need to switch to a new API like Google or DuckDuckGo
Microsoft Closing Azure DataMarket
// Old Answer
The way the new Bing API works is different, but you can use it without logging in, by base64 encoding your AppId and then setting it as the "Basic" authorization in the headers. I got the idea from this answer on stackoverflow. The trick is that you need to add a colon at the beginning of your AppId before base64 encoding it.
Here is a working example I made that searches Bing and returns a random image from the results. If you want to make it work, just add your own AppId:
'use strict';
$(document).ready(function() {
//Declare variables
var $searchButton = $('#searchButton');
//add a colon to the beginning of your AppId string
var appId = ':TZYNotARealAppId';
//Function to get images
function getImage() {
//base64 encode the AppId
var azureKey = btoa(appId);
//get the value from the search box
var $searchQuery = $('#searchBox').val();
//Create the search string
var myUrl = 'https://api.datamarket.azure.com/Bing/Search/v1/Composite?Sources=%27image%27&$top=50&$format=json&Query=%27' + $searchQuery + '%27';
//Make post request to bing
$.ajax({
method: 'post',
url: myUrl,
//Set headers to authorize search with Bing
headers: {
'Authorization': 'Basic ' + azureKey
},
success: function(data) {
//Insert random image in dom
var randomIndex = Math.floor(Math.random() * 50);
var imgLink = '<img width="500px" src="' + data.d.results[0].Image[randomIndex].MediaUrl + '" />';
$('#output').html(imgLink);
},
failure: function(err) {
console.error(err);
}
});
};
//Trigger function when button is clicked
$searchButton.click(function(e) {
e.preventDefault();
getImage();
});
});
<html>
<head>
<title>Image search widget</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="test search widget" />
</head>
<body>
<main>
<form>
<input id="searchBox" type="text" type="submit" name="searchBox" required/>
<button id="searchButton" type="submit">Get Image</button>
</form>
<div id="output"></div>
</main>
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="./js/search.js"></script>
</body>
</html>
user3809384
Updated on June 13, 2022Comments
-
user3809384 about 2 years
At the moment i am only able to do my searches based on logging in to datamarket azure.
Results returned are formatted in a table form and i dont fidn any way to return them in JSON format.
A link is displayed after results are returned but when that link is pasted in the URL section of a browser it requires a username and a password.
Example of returned URL https://api.datamarket.azure.com/Bing/Search/v1/Web?Query=%27car%27
There used to be an api Using REST for it but now it only return errors and is no longer working.
Is there any way to use this BING API and retrieve it's return queries?
Returned Error after failing to attempt to log in to azure The authorization type you provided is not supported. Only Basic and OAuth are supported
-
user3809384 over 9 yearsit still requires a log in from me which when i have inputted my hotmail account it doesnt work. When i proceed to click on cancel it then returns me : Returned Error after failing to attempt to log in to azure The authorization type you provided is not supported. Only Basic and OAuth are supported bing
-
pista329 about 9 yearsFor authentification... use empty login and your primary account key as password.
-
Bitanshu Das over 8 years@BurhanKhalid is there a way to do site specific query like I want to obtain search results just from Linkedin?
-
Admin almost 8 yearsThis method still works. I've created a fiddle just in case someone wants to experiment. jsfiddle.net/skd/28toz2n2