如何从BING搜索引擎API返回JSON结果

问题描述:

目前我只能根据登录到datamarket azure的方式进行搜索。如何从BING搜索引擎API返回JSON结果

返回的结果格式化为表格形式,我不想以任何方式以JSON格式返回它们。

返回结果后显示链接,但当链接粘贴到浏览器的URL部分时,它需要用户名和密码。返回URL的

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

曾经有一个API,使用它REST,但现在它只返回错误并且不再工作。

有什么方法可以使用这个BING API并检索它的返回查询吗?

未能尝试登录到azure后返回错误 您提供的授权类型不受支持。只有基本和OAuth支持

您需要从URL中删除v1,并添加$format=json到您的查询的结束,详见在schema guide

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

要得到的链接工作,您需要提供您的哈希凭据,以获取该信息,请按照下列步骤操作:

  1. 登录到Azure市场。
  2. 浏览到search api listing
  3. 单击“浏览此数据集”链接(仅在您登录时才可见)。
  4. 一旦打开,就“秀”上点击旁边的“主帐号键” enter image description here
  5. 保存这个散列值,并在你的代码验证摘要使用它。
+0

它仍然需要一个日志从我这当我输入它不工作我的Hotmail帐户。 当我继续点击取消,然后返回我:未能尝试登录到Azure您提供的授权类型不支持后 返回的错误。只支持Basic和OAuth bing – user3809384 2014-11-10 02:45:22

+0

you da man !!谢谢你 – user3809384 2014-11-10 07:01:13

+1

验证...使用空登录名和你的主帐号密码作为密码。 – pista329 2015-05-28 09:12:45

新的Bing API的工作方式不同,但您可以在不登录的情况下使用它,通过base64编码您的AppId并将其设置为标头中的“基本”授权。我从this answer on stackoverflow得到了这个想法。诀窍是你需要在base64编码之前在你的AppId的开头添加一个冒号。

这是一个工作示例,我用它搜索Bing并从结果中返回一个随机图像。如果你想它的工作,只需添加自己的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>

+0

这种方法仍然有效。我创造了一个小提琴,以防万一有人想实验。 https://jsfiddle.net/skd/28toz2n2/ – 2016-08-09 13:29:53