Bing News Search API With Universal Windows Platform

Today, we are going to talk about Bing News Search API of Cognitive Toolkit on Universal Windows Platform, using Apache Cordova cross platform technology.

First, we need to get LUIS API for Bing News Search.

Go to this site and create an account. Use your Azure subscription account.

https://www.microsoft.com/cognitive-services/en-US/subscriptions


Figure: 1

Now, select Bing News from the list given above and get API for Bing News Search API. Afterwards, you can see your Free Bing Search API information from the list given below.


Figure: 2

You can select any Cognitive toolkit API from the link given below.

https://www.microsoft.com/cognitive-services/en-us/Bing-news-search-API/documentation/useanddisplayrequirements


Figure: 3

There is a tool to test different API's of Cognitive toolkit. You can see the tool at the link given below.

https://dev.cognitive.microsoft.com/docs/services/56b43f72cf5ff8098cef380a/operations/56f02400dbe2d91900c68553/console


Figure: 4

Here, on this tool, you need to select Category and Ocp-Apim-Subscription-Key. You can get the subscription key from Cognitive toolkit API site.


Figure: 5

Here, you can see after selecting Category and entering Ocp Subscription Key. You can try to access API and see the response.


Figure: 6

From the response, you can see Response status: 200 OK and you can see JSON object of the response.


Figure: 7

Now, create a Universal Windows Platform app, using Apache Cordova toolkit.

Replace the code of an index page with the code given below. 

  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <title>JSSample</title>  
  5. http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js  
  6. </head>  
  7. <body>  
  8.   
  9. $(function() {  
  10. var params = {  
  11. // Request parameters  
  12. "Category""Sports"  
  13. };  
  14.   
  15. $.ajax({  
  16. url: "https://api.cognitive.microsoft.com/bing/v5.0/news/?" + $.param(params),  
  17. beforeSend: function(xhrObj){  
  18. // Request headers  
  19. xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key","08a7d09e159a4af6b052ce2b97a0f2ae");  
  20. },  
  21. type: "GET",  
  22. // Request body  
  23. data: "{body}",  
  24. })  
  25. .done(function(data) {  
  26. //alert("success");  
  27. console.log(data);  
  28. $("#type").html(data._type);  
  29. $("#category").html(data.value[0].category);  
  30. $("#provider").html(data.value[0].provider[0].name);  
  31. $("#date").html(data.value[0].datePublished);  
  32. $("#name").html(data.value[0].name);  
  33. $("#description").html(data.value[0].description);  
  34. //document.getElementById("description").innerHTML = JSON.stringify(data.value[0].description, undefined, 2);  
  35. })  
  36. .fail(function() {  
  37. alert("error");  
  38. });  
  39.   
  40. $('#btn').click(function() {  
  41. alert("Hello");  
  42. });  
  43. });  
  44.   
  45. <h2>Bing News Search API</h2>  
  46. <h3 id="type"></h3>  
  47. <p id="category"></p>  
  48. <p id="provider"></p>  
  49. <p id="date"></p>  
  50. <p id="name"></p>  
  51. <p id="description"></p>  
  52. <input id="btn" type="button" value="button" />  
  53. <pre id="json"></pre>  
  54. </body>  
  55. </html>   

Now, run Universal Windows Platform app and see the result, as shown below.

Here, you can see the category, provider info, news type which is Sports, published data, name header and description.

output


Similar Articles