Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Searching for News Headlines7:11 with Carling Kirk
We'll use the Bing News Search API to get news headlines.
Now that we know how to use the web client class to retrieve data from a web page, 0:00 we can use the Bing search API to retrieve data. 0:04 We'll take our top ten players of the MLS and then search for 0:08 news headlines about each player. 0:11 Earlier we use the web client class with just a URL but 0:14 this time we'll need to send our API key with our request. 0:18 We'll do that with headers. 0:21 You might have learned about headers with us before, 0:23 they are extra pieces of information about a web request or 0:25 response that includes additional information about the message. 0:28 >> Let's take a look at the Bing News Search API documentation. 0:33 Here, we've got a little preview tool. 0:38 We can click on one of these examples. 0:41 And it looks like we've got results to the right. 0:44 Let's take a look at the JSON. 0:46 We start out with a curly brace. 0:49 That means that this entire JSON response is one object. 0:51 And looks like the results are in this property named value. 0:57 It's got an array, I can tell because of this bracket. 1:01 Name, it looks like that's a headline. 1:04 And description, that's the summary. 1:07 Let's go back up and click on Documentation. 1:12 Let's see, to get started read our Getting Started guide 1:16 which describes how you can obtain your own subscription keys. 1:19 I've already got my API key, so if you haven't already and 1:23 wanna follow along make sure to pause the video now. 1:26 Click on the Getting Started link right here and follow the instructions. 1:30 Use of these APIs are free under a certain number of requests, so keep that in mind. 1:34 For information that shows you how to use the API, see News Guide. 1:40 All right, let's see we've got three URLs we can use. 1:44 This second one here looks like the one we should use for 1:48 searching on soccer player names. 1:52 Let's see if we can find an example. 1:55 Getting news articles that are relevant to a search query. 1:58 All right. 2:01 Get, here's what our URL should look like when we make the request. 2:05 And here's what our API key header will look like. 2:08 And we've got a sample of the response. 2:12 I think this is all we need to get started. 2:15 We'll need a class first so we can deserialize this data. 2:17 We can copy this JSON response. 2:20 And we'll use our paste JSON as classes trick we did earlier. 2:23 We'll reference this page later for 2:29 the URL in our API key once we get into using web client. 2:31 Back in our code we'll click at class and let's name a new search. 2:35 Okay, we've got a response in the clipboard. 2:46 So I'll highlight this. 2:48 Click Edit. 2:50 Paste Special. 2:51 Paste JSON as Classes. 2:52 Back in the day, we used to have to type all this out by hand. 2:56 Let's rename RootObject to NewSearch. 3:00 Now we can go back to our program class and 3:04 create a method to retrieve our new search results. 3:07 Down here at the bottom we can copy and paste our Get Google home page method. 3:11 We need to make a request for each one of our players, 3:22 so let's call it get news for player. 3:25 GetNewsForPlayer, and 3:28 it'll take a string parameter called playerName. 3:31 For now we'll focus on just getting the string result before we deserialize. 3:38 Let's change the variable name to, searchResults. 3:43 And change it down here, too. 3:49 Now we need the URL, we'll go copy it from the page. 3:53 All right this, right here and 3:58 We'll need to replace the search parameter in the your URL, though. 4:04 We can use a method on the string class called format. 4:08 It returns a string and accepts additional parameters, 4:11 that it inserts into the string it returns. 4:14 Let's see how it works. 4:17 Place Google with string.format. 4:18 The first parameter is the composite string in which it will insert values. 4:25 Which is our URL. 4:29 Paste that there. 4:32 Then we'll replace the text after Q equals with an indexed placeholder. 4:34 Which is specified by curly braces and an index number. 4:41 Curly brace zero, curly brace. 4:45 Then we'll pass the player name as the second parameter into 4:48 the string.format method. 4:53 PlayerName, then we enclose the string.format method. 4:55 The placeholder here is telling string.format to insert the value 5:01 of the playerName variable. 5:06 There are lots of things you can do with this method. 5:08 It's especially useful for formatting date time values and 5:10 decimal numbers into culture specific formatting. 5:13 For more reading on this method, check out the documentation and the notes. 5:17 Next we need to get our API key in the headers. 5:21 You got your key ready? 5:24 You won't be able to use the one I am about to use, because it will be inactive. 5:25 First, we'll need to add our header, 5:29 webClient.Headers.Add. 5:35 This add method has a few overloads. 5:39 One of them uses an enum that has some predefined headers that are often used. 5:42 Except as a common one. 5:48 It tells the endpoint what type of data we want. 5:50 So if the service we're requesting can either send JSON or XML we'd specify JSON. 5:53 In our case the default is JSON so we won't need it. 5:59 The header we need to use though is a custom header and 6:02 it won't be on this list. 6:05 We need to use the last overload here which just takes two strings one for 6:09 name and one for value. 6:14 We'll go back to the documentation and grab the header name we need. 6:17 All right, and we can paste that in here, then I'm gonna paste in my API key. 6:25 Okay, that should be all we need to make this web request. 6:35 Let's try it out in main. 6:39 I'll replace this GetGoogleHomePage with our new method. 6:44 GetNewsForPlayer and I'll pass it Diego Valari. 6:47 And one more parenthesis. 6:55 Okay, let's run with control F5. 6:58 And we've got some data. 7:03 Next step we'll need to deserialize these results into our new search class. 7:05 [BLANK-AUDIO] 7:10
You need to sign up for Treehouse in order to download course files.Sign up