Spread the love
In this tutorial we are going to see how to use the Bing News Search API to get results. The Bing News Search API is a RESTful service that helps search the web for news articles. Results include details like authoritative image of the news article, related news and categories, provider info, article URL, and date added. With the API v7, you can have sorting and filtering options that simplify your news search.
Prerequisites
- To run the sample code you must have Visual Studio installed.
- You will need the Json.NET NuGet package.
- To use the Bing Web Search API you need to create a Cognitive Services API account with access to the Bing Search APIs. If you don’t have an Azure subscription, you can create a free account.
Create a new project
To make a Search Request follow the steps below:
- Create a Console Application in Visual Studio 2017
- Add the JSON.net nuget package
Install-Package Newtonsoft.Json
- Add the following code:
namespace BingNewsSearch { class Program { const string subscriptionKey = "enter your key here"; const string uriBase = "https://api.cognitive.microsoft.com/bing/v7.0/news/search"; const string searchTerm = "CodeStories"; struct SearchResult { public String jsonResult; public Dictionary<String, String> relevantHeaders; } static void Main(string[] args) { SearchResult result = BingNewsSearch(searchTerm); //deserialize the JSON response dynamic jsonObj = Newtonsoft.Json.JsonConvert.DeserializeObject(result.jsonResult); Console.WriteLine(jsonObj["value"][0]); Console.ReadKey(); } static SearchResult BingNewsSearch(string toSearch) { var uriQuery = uriBase + "?q=" + Uri.EscapeDataString(toSearch); WebRequest request = WebRequest.Create(uriQuery); request.Headers["Ocp-Apim-Subscription-Key"] = subscriptionKey; HttpWebResponse response = (HttpWebResponse)request.GetResponseAsync().Result; string json = new StreamReader(response.GetResponseStream()).ReadToEnd(); // Create the result object for return var searchResult = new SearchResult() { jsonResult = json, relevantHeaders = new Dictionary<String, String>() }; // Extract Bing HTTP headers foreach (String header in response.Headers) { if (header.StartsWith("BingAPIs-") || header.StartsWith("X-MSEdge-")) searchResult.relevantHeaders[header] = response.Headers[header]; } return searchResult; } } }
- Replace the const string subscriptionKey = “enter your key here”; with your subscription key
- Run the program
Get the Results
Below is the JSON response with our search results.
{ "_type": "News", "readLink": "https://api.cognitive.microsoft.com/api/v7/news/search?q=codestories", "totalEstimatedMatches": 16, "value": [ { "about": null, "provider": [ { "_type": "Organization", "name": "WBUR" } ], "datePublished": "2011-08-08T00:00:00Z", "clusteredArticles": null, "mentions": null, "video": null, "category": null, "name": "ZIP-Code Stories Looks For Your Writing", "url": "http://www.wbur.org/radioboston/2011/08/08/zip-code-stories", "description": "Author Daphne Kalotay has lived in Brookline since the early 1990s, so it's natural that the town seeped into her work. Her story \"Sunshine Cleaners\" isn't about Brookline per se, she said, it's about her community. Each month, we'll pick four ZIP codes ...", "image": null }, { "about": null, "provider": [ { "_type": "Organization", "name": "DZone" } ], "datePublished": "2012-10-31T00:00:00Z", "clusteredArticles": null, "mentions": null, "video": null, "category": null, "name": "Code Stories: Patrick Wyatt, Game Programmer and Producer of Warcraft", "url": "https://dzone.com/articles/code-stories-patrick-wyatt", "description": "Join the DZone community and get the full member experience. Pat was kind enough to help us inaugurate Code Stories, a new series of conversations at DZone. We'll chat with coders from all walks of life about how they got started, where they are now, and ...", "image": null }, { "about": [ { "readLink": "https://api.cognitive.microsoft.com/api/v7/entities/7b91f106-198a-5880-55b1-54852b147d96", "name": "Zip Code" } ], "provider": [ { "_type": "Organization", "name": "Newser" } ], "datePublished": "2013-07-01T17:16:00Z", "clusteredArticles": null, "mentions": null, "video": null, "category": null, "name": "Why Do Stores Keep Asking for Your ZIP Code?", "url": "http://www.newser.com/story/170079/why-do-stores-keep-asking-for-your-zip-code.html", "description": "\"We strictly utilize the information we receive to better understand the demographics of the market of those specific ZIP codes,\" says a spokesperson from Las Vegas' Mob Museum. (Read more zip code stories.)", "image": null }, { "about": null, "provider": [ { "_type": "Organization", "name": "Adelaide Crows" } ], "datePublished": "2018-12-20T06:23:00Z", "clusteredArticles": null, "mentions": null, "video": null, "category": null, "name": "Hugh Greenwood's return to basketball", "url": "http://www.afc.com.au/news/2018-12-20/hughs-return-to-basketball", "description": "The 26-year-old has been one of the Crows’ biggest cross-code stories since switching to Aussie Rules football in 2015, but basketball will always be his first love. More than just a sport, it was something that bound him to his late mother, Andree.", "image": null }, { "about": null, "provider": [ { "_type": "Organization", "name": "MTV" } ], "datePublished": "2015-05-29T00:26:00Z", "clusteredArticles": null, "mentions": null, "video": null, "category": null, "name": "Meet The Florida Teen Who Lost Her Honor Society Title Over One Dress", "url": "http://www.mtv.com/news/2172213/teen-dress-honor-title-stripped/", "description": "If this is the worst thing that’s happened to you in your life, consider yourself lucky.” MTV: These dress code stories are unfortunately so commonplace right now. So many young women and men are fighting back these unfair policies, and we're only just ...", "image": null }, { "about": null, "provider": [ { "_type": "Organization", "name": "Missoulian" } ], "datePublished": "2016-10-13T22:18:00Z", "clusteredArticles": null, "mentions": null, "video": null, "category": null, "name": "Review: 'Deep Code' delves into tragedy and oddity", "url": "https://missoulian.com/review-deep-code-delves-into-tragedy-and-oddity/article_7e102726-1502-5280-9ca1-dc15655f08af.html", "description": "She is an avid painter, thrift-shopper and adventurer. Charley Henley will have a reading and signing for \"The Deep Code: Stories\" at 7 p.m. Wednesday, Oct. 18, at Fact & Fiction Downtown, 220 N. Higgins Ave.", "image": null }, ... ...
Find the complete Code on my Github in this repository.
Hello
Which control will i use to show the results of the search and how?
Do you have any example of that?
Thank you