How do i return JSON results from BING Search Engine API

本秂侑毒 提交于 2020-01-01 03:41:07

问题


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


回答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:

  1. Login to the Azure Marketplace.
  2. Browse to the search api listing
  3. Click on the "Explore this Dataset" link (its only visible if you are logged in).
  4. Once it opens, on the top click on "Show" next to "Primary Account Key"

  5. Save this hashed value and use it in your code as the authentication digest.



回答2:


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>


来源:https://stackoverflow.com/questions/26732530/how-do-i-return-json-results-from-bing-search-engine-api

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!