2014-11-04 228 views
3

目前我只能根據登錄到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支持

回答

7

您需要從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

3

新的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