Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,270 --> 00:00:07,860
Now in the last lesson, we saw how we could work with the kanye.rest API and the joke API to start querying
2
00:00:07,920 --> 00:00:12,330
these external web sites for pieces of data that they may have.
3
00:00:12,330 --> 00:00:18,810
We saw how we could use paths as well as parameters to narrow down on the exact type of data that we
4
00:00:18,810 --> 00:00:19,600
want.
5
00:00:19,740 --> 00:00:28,070
And we saw how we can make these requests using a formatted URL and putting it through our browser.
6
00:00:28,170 --> 00:00:36,000
Now both of those web sites had very simple pieces of data, either a database of jokes or a database of
7
00:00:36,000 --> 00:00:37,950
Kanye West quotes.
8
00:00:37,950 --> 00:00:45,990
Now when we come to data that's more monetizable, or allows developers to build more complex applications
9
00:00:46,290 --> 00:00:49,990
that might be used by hundreds or thousands of users,
10
00:00:50,230 --> 00:00:58,050
then these web sites have to start thinking very carefully about how to either monetize your use of
11
00:00:58,050 --> 00:01:03,780
their data or how to limit your use to a threshold.
12
00:01:04,080 --> 00:01:08,700
And the way that they would do that is through authentication.
13
00:01:08,700 --> 00:01:16,530
So every time you make a request through the API, they have to be able to identify you as the developer,
14
00:01:17,100 --> 00:01:24,630
and they have to keep track of how often you're using their server to get data, and then charge you, or
15
00:01:24,630 --> 00:01:30,290
limit you, accordingly. In order to illustrate this concept of authentication,
16
00:01:30,300 --> 00:01:32,490
weâre going to use the openweathermap.
17
00:01:32,550 --> 00:01:34,860
orgâs API.
18
00:01:34,860 --> 00:01:42,000
And if you take a look in their pricing, they tell you that it's free to use their API as long as you
19
00:01:42,000 --> 00:01:50,670
don't make more than sixty requests per minute. So every single time we typed in this particular request
20
00:01:51,000 --> 00:01:53,970
to an API and we hit enter,
21
00:01:53,970 --> 00:02:00,420
thatâs a single request. And if you make more than 60 a minute, so more than one a second, then they're
22
00:02:00,420 --> 00:02:04,100
going to limit any further requests.
23
00:02:04,140 --> 00:02:10,940
So for most developers to get started building your app or your web site, this is more than enough.
24
00:02:11,370 --> 00:02:17,130
But as you start getting more users, you have more traffic, and you're probably likely to have more revenue,
25
00:02:17,550 --> 00:02:23,540
then these data providers will also start charging you to use their data.
26
00:02:23,880 --> 00:02:30,440
But the way that we would implement authentication in any of these categories is exactly the same.
27
00:02:30,480 --> 00:02:35,160
So let's see how we can use an authenticated API like this one.
28
00:02:35,160 --> 00:02:39,630
So the first thing to do is to go ahead and sign up to the OpenWeatherMap,
29
00:02:39,630 --> 00:02:41,070
if you haven't already.
30
00:02:41,070 --> 00:02:42,890
So you're going to create a new account.
31
00:02:42,960 --> 00:02:49,690
And don't worry it doesn't require any credit card details or anything other than a user name and email.
32
00:02:49,690 --> 00:02:54,300
Now once you've signed up then go ahead and sign into your account.
33
00:02:54,720 --> 00:02:58,730
And once you're signed in you should see a page that looks like this.
34
00:02:58,830 --> 00:03:05,040
And up here there are a whole bunch of tabs and you want to tab over to the API key section.
35
00:03:05,130 --> 00:03:08,410
Now here you can create a new key
36
00:03:08,580 --> 00:03:15,570
if you don't see a default one here. Once you've created a key, then you're just simply going to copy
37
00:03:15,930 --> 00:03:22,930
this entire string, and you're going to be using it when you make any requests to the API.
38
00:03:23,040 --> 00:03:27,220
So let's see how we can make a request to this weather API.
39
00:03:27,360 --> 00:03:32,160
Let's take a look at the API docs for getting the current weather data.
40
00:03:32,160 --> 00:03:36,840
Now if you read through this documentation, you'll see that there's a whole bunch of ways that you can
41
00:03:36,840 --> 00:03:45,630
get weather data, either querying by city name, or by a city ID, or by the geographic latitude and longitude
42
00:03:45,630 --> 00:03:47,790
values, and a whole lot more.
43
00:03:48,390 --> 00:03:53,570
Now let's say that I want to use the simplest, which is to query by a city name.
44
00:03:53,760 --> 00:04:01,590
Then you can see, if we click on the example API call, that they've got some sort of end point that ends
45
00:04:01,590 --> 00:04:05,310
here, and then begin the parameters,
46
00:04:05,310 --> 00:04:05,800
right,
47
00:04:05,850 --> 00:04:09,010
because we said the parameters start with a question mark.
48
00:04:09,050 --> 00:04:14,490
So the first parameter has a key of q, which probably stands for query, I would guess,
49
00:04:14,490 --> 00:04:22,110
and then the name of the city, and then the next parameter is the app ID, which corresponds to the API
50
00:04:22,110 --> 00:04:23,580
key that we've got just now
51
00:04:23,580 --> 00:04:24,850
when we signed up.
52
00:04:24,960 --> 00:04:30,950
So this, overall, is the structure of this sample API call.
53
00:04:30,960 --> 00:04:37,190
Now the reason why itâs a sample is because here they provided you a demo API key.
54
00:04:37,290 --> 00:04:44,730
Now in order to make an actual API call, we have to look at that actual endpoint, and it actually looks
55
00:04:44,850 --> 00:04:49,440
more like this. Instead of samples.openweathermap,
56
00:04:49,530 --> 00:04:52,040
itâs actually api.openweathermap.
57
00:04:52,620 --> 00:04:59,830
So if I change this from samples to api, then you'll see that this won't work
58
00:05:00,160 --> 00:05:07,390
with the app ID that they provided by default, and we get the error message âInvalid API Keyâ.
59
00:05:08,140 --> 00:05:14,440
Now remember that earlier on we already signed up and we actually got our very own app ID, and you
60
00:05:14,440 --> 00:05:15,470
should have done this as well.
61
00:05:15,860 --> 00:05:20,950
Well in that case, if you simply paste that app ID here, after the equals sign,
62
00:05:20,950 --> 00:05:24,190
so this is the key value pair here,
63
00:05:24,220 --> 00:05:28,150
then we go ahead and make our request by hitting enter,
64
00:05:28,210 --> 00:05:35,480
you can see we get our data back. And this is the actual data for the current weather in London.
65
00:05:35,740 --> 00:05:40,380
And as you can see, as usual, there's a few clouds in the sky.
66
00:05:40,420 --> 00:05:47,020
This is the default weather for London pretty much, but it's not too bad.
67
00:05:47,020 --> 00:05:52,090
You can try changing this q to a different city.
68
00:05:52,090 --> 00:05:59,380
Let's check what the weather is like in Paris. And we get some temperatures back, and we get some wind
69
00:05:59,380 --> 00:06:00,370
speeds.
70
00:06:00,550 --> 00:06:07,530
We get humidity and atmospheric pressure. But you might notice that the temperature is kind of weird, right?
71
00:06:07,540 --> 00:06:09,610
It seems like Paris is burning.
72
00:06:09,610 --> 00:06:12,130
283 degrees.
73
00:06:12,130 --> 00:06:15,640
Even if that's Fahrenheit, that's still too high.
74
00:06:15,640 --> 00:06:23,130
What's going on here? Their default unit for temperature for OpenWeatherMap is actually Kelvins.
75
00:06:23,410 --> 00:06:32,560
And in order to get either imperial, so Fahrenheit, or metric, Celsius, we have to add another parameter.
76
00:06:33,320 --> 00:06:37,090
So depending on whether if your brain works in metric or imperial,
77
00:06:37,270 --> 00:06:45,070
go ahead and try to add these parameters to our API request, so that you get back the temperature in
78
00:06:45,070 --> 00:06:49,120
a format that you understand. Pause the video, and give that a go.
79
00:06:52,380 --> 00:07:00,480
All right. So we know that the first parameter gets added after a question mark, and it has to be added
80
00:07:00,540 --> 00:07:10,410
in a key value pair separated by an equals sign, but every subsequent parameter gets added after an ampersand.
81
00:07:10,410 --> 00:07:15,300
So if we want to add another parameter here, we're also going to add an ampersand,
82
00:07:15,300 --> 00:07:22,050
and then the name or the key of the parameter is called units, and then the value could be imperial or
83
00:07:22,050 --> 00:07:23,160
metric.
84
00:07:23,160 --> 00:07:29,890
So I'm going to add âunits=metricâ because I want the results back in Celsius.
85
00:07:30,000 --> 00:07:33,850
And you can see that Paris is currently 10.3 degrees.
86
00:07:33,870 --> 00:07:37,170
That sounds a lot more reasonable.
87
00:07:37,260 --> 00:07:41,670
Now remember that the order at which you put these parameters doesn't matter.
88
00:07:41,700 --> 00:07:52,320
So, for example, I could have the query first, which is Paris, and then have my units, which is metric, and
89
00:07:52,320 --> 00:07:54,950
finally have my app ID like this, and
90
00:07:55,080 --> 00:07:56,890
I still get the same results.
91
00:07:56,910 --> 00:07:59,010
The order doesn't matter at all,
92
00:07:59,130 --> 00:08:04,410
as long as they're separated by these ampersands, or, if it's right at the beginning, the question mark.
93
00:08:05,390 --> 00:08:12,860
Now notice how, as we start using more parameters, and especially when we have parameters with very long
94
00:08:12,860 --> 00:08:21,490
values like this, it gets very difficult to test our APIs using a browser, and editing these
95
00:08:21,490 --> 00:08:21,950
URLs.
96
00:08:21,980 --> 00:08:28,520
It's very very fiddly, and you can often make very simple typos, because it's hard to see where each parameter
97
00:08:28,550 --> 00:08:31,210
ends and the next one begins.
98
00:08:31,250 --> 00:08:37,520
So very often, when we're testing APIs, we'll use a tool called Postman.
99
00:08:37,549 --> 00:08:43,340
It's completely free to download, and it's available for Mac, Windows and Linux.
100
00:08:43,340 --> 00:08:48,980
So I recommend heading over to postman.com/downloads, and download the version that's right
101
00:08:48,980 --> 00:08:50,690
for your computer.
102
00:08:50,990 --> 00:08:56,090
Now once you've done that, you'll end up with an application that looks something like this.
103
00:08:56,210 --> 00:09:00,230
So let's see how we can make that same request using Postman.
104
00:09:00,650 --> 00:09:06,650
So I'm going to click on this plus sign to make a new request, and it's going to be a get request.
105
00:09:06,650 --> 00:09:12,200
I'm going to try and get some data from our OpenWeatherMap. And I'm basically going to try and do the
106
00:09:12,200 --> 00:09:13,610
same thing as before,
107
00:09:13,700 --> 00:09:21,080
so I'm going to use the API end point, which goes up to the first question mark, and I'm going to paste
108
00:09:21,080 --> 00:09:25,970
that in the URL, and then I get to add all my parameters.
109
00:09:25,970 --> 00:09:33,050
So the first parameter is q, because that's the parameter to query by city name.
110
00:09:33,050 --> 00:09:42,260
Now if you wanted to query by latitude, longitude, or by a zip code, then you can use these different parameters.
111
00:09:42,380 --> 00:09:45,600
But in my case I'm just going to stick with the simplest.
112
00:09:45,710 --> 00:09:51,040
So I'm going to put the key as q and the value as London.
113
00:09:51,050 --> 00:09:57,740
Notice how at the top here, it's starting to structure my URL for me, while I get to work with this much
114
00:09:57,770 --> 00:10:05,720
clearer user interface, which has a table, and has like a checkbox for me to add or remove, and it saves
115
00:10:05,720 --> 00:10:08,830
this data if I want to use it later on, etc..
116
00:10:08,900 --> 00:10:16,250
So let's add the next piece of data, which is our API key. And remember that the API keyâs parameter
117
00:10:16,370 --> 00:10:18,010
is called appid.
118
00:10:18,230 --> 00:10:24,470
So let's add the app ID and paste in the API key, and it's going to again add that to my parameter
119
00:10:24,470 --> 00:10:25,370
list.
120
00:10:25,370 --> 00:10:29,750
And the final thing I'm going to do is I'm going to change the units to metric.
121
00:10:29,750 --> 00:10:37,700
Now I'm going to click âSendâ to send this request to OpenWeatherMap, and I get back my data.
122
00:10:37,700 --> 00:10:44,080
Now notice how this data here is structured in a much better way,
123
00:10:44,210 --> 00:10:52,100
and the reason is because we are pretty printing the results, instead of, well, ugly printing the results.
124
00:10:52,470 --> 00:11:00,110
What's happening here is, when we make our request to the OpenWeatherMap servers, the data that we get
125
00:11:00,110 --> 00:11:04,760
back is in something called a JSON format.
126
00:11:04,760 --> 00:11:07,820
And what exactly is a JSON format?
127
00:11:08,240 --> 00:11:11,240
Well, you get to find that out in the next lesson.
128
00:11:11,360 --> 00:11:18,290
So make sure that you've signed up for a account on OpenWeatherMap, and you've had practice using Postman
129
00:11:18,380 --> 00:11:23,930
as well as using your browser to authenticate yourself with the OpenWeatherMap API, and youâre getting
130
00:11:23,930 --> 00:11:26,270
the data back in a JSON format.
131
00:11:26,420 --> 00:11:32,480
Once you've done that, head over to the next lesson, and we'll find out more about the format of the data
132
00:11:32,510 --> 00:11:33,320
that we get back.
14403
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.