How do I return JSON results from the BING Search Engine API

At the moment I can only perform my searches based on datamarket azure login.

The resulting results are formatted in table form and I don't want to return them in JSON format.

The link will be displayed after the results are returned, but when this link is inserted in the URL section of the browser, it will require a username and password.

Example url returned https://api.datamarket.azure.com/Bing/Search/v1/Web?Query=%27car%27

It used to be an api using REST, but now it only returns errors and doesn't work anymore.

Is there a way to use this BING API and get its returned requests?

Error returned after attempting an azure login. The authorization type you provided is not supported. Only Basic and OAuth are supported.

+3


source to share


2 answers


You need to remove v1

from your url and add $format=json

to the end of your request as described in the schema :

https://api.datamarket.azure.com/Bing/Search/Web?Query=%27Xbox%27&$format=json

      



To get the link to work, you have to provide it with your hashed credentials, to get this follow these steps:

  • Enter the Azure Market.
  • Go to the api search list
  • Click the Explore this dataset link (only visible if you're signed in).
  • Once it opens, at the top, click "Show" next to "Account Primary Key" enter image description here
  • Save this hashed value and use it as an authentication digest in your code.
+7


source


How the new Bing API works, but you can use it without logging in by base64 encoding your AppId and then setting it as "basic" authorization in the headers. I got the idea fooobar.com/questions/1566334 / ... . The trick is that you need to add a colon at the beginning of your AppId, before it's base64 encoded.

Here is a working example I made for Bing search 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>
      

Run codeHide result


+3


source







All Articles