WEBVTT

0
00:00.360 --> 00:03.090
Previously we completed step one

1
00:03.330 --> 00:06.960
which is setting up a user account on Pixela.

2
00:07.590 --> 00:09.360
Now we can move on to step two

3
00:09.450 --> 00:13.920
which is to create a new graph on Pixela for our username.

4
00:14.550 --> 00:19.550
So notice how the endpoint is now different because in addition to /

5
00:20.160 --> 00:21.210
v1/users

6
00:21.480 --> 00:26.480
we also have to add our username now and then for /graphs.

7
00:27.030 --> 00:31.590
So let's go back to the documentation and scroll down to the relevant part of

8
00:31.590 --> 00:36.030
the API, which is this part to create a graph, that's step two.

9
00:37.410 --> 00:40.890
This is also going to be a HTTP post request,

10
00:41.310 --> 00:43.770
but the end point has now changed.

11
00:44.280 --> 00:47.160
So let's go ahead and create our new endpoint

12
00:47.580 --> 00:49.770
which is the graph endpoint.

13
00:51.750 --> 00:56.340
And this is going to be comprised of the Pixela endpoint

14
00:56.700 --> 01:00.780
and then in addition, it's going to be / our username.

15
01:01.770 --> 01:03.090
So let's take out our

16
01:03.090 --> 01:07.050
username and our token and put it as a constant at the top.

17
01:07.130 --> 01:07.963
<v 1>Right</v>

18
01:14.840 --> 01:17.720
<v 0>Now we can add that in here using an f-string</v>

19
01:20.630 --> 01:23.300
and then finally it's /graphs.

20
01:25.700 --> 01:27.920
Now, remember if you're doing this along with me,

21
01:27.950 --> 01:32.750
this username is going to have to be your own unique username and the token is

22
01:32.750 --> 01:34.700
going to be your own secret token.

23
01:36.230 --> 01:38.360
Once we've created our graph end point,

24
01:38.600 --> 01:43.130
the next step is to make our post request. Again,

25
01:43.160 --> 01:48.160
we're going to use our request module and call the post method to make this

26
01:48.200 --> 01:52.760
request. This time what is the data that we want to send over?

27
01:53.330 --> 01:53.690
Well,

28
01:53.690 --> 01:58.690
we want to be able to add the ID of our new graph,

29
01:59.300 --> 02:04.300
so some way of identifying it in the URL, and then the name of the graph,

30
02:05.090 --> 02:09.590
and then the unit of measure that we're keeping track of. For example,

31
02:09.590 --> 02:11.600
if I'm keeping track of my cycling,

32
02:11.840 --> 02:14.540
then that's going to be kilometers that I've cycled,

33
02:14.870 --> 02:18.950
or if I want to keep track of how long I'm coding

34
02:18.980 --> 02:21.320
then that might be minutes or hours.

35
02:21.950 --> 02:25.850
And then the data type of the unit of measure.

36
02:26.240 --> 02:29.930
If I was tracking the number of pages I've read then they'll be whole numbers,

37
02:30.320 --> 02:32.660
if I'm tracking the number of kilometers I've ridden

38
02:32.900 --> 02:36.470
then it's probably going to be a floating point number. And then finally,

39
02:36.500 --> 02:39.530
we're going to specify the color of our graph. Here

40
02:39.530 --> 02:42.950
you have to be a little bit careful because these are strings

41
02:42.980 --> 02:46.160
which are actually in Japanese. So for example,

42
02:46.580 --> 02:48.680
momiji is autumn leaves.

43
02:49.250 --> 02:52.580
shibafu is grass, sara is sky,

44
02:53.300 --> 02:58.300
and they've got a whole bunch of poetic names for each of these colors,

45
02:58.790 --> 03:01.120
which wonderful, but it also prone to errors.

46
03:01.180 --> 03:05.590
So you have to be careful when you're typing in the color that you want.

47
03:05.800 --> 03:09.640
And if in doubt, just simply copy it from the documentation.

48
03:10.930 --> 03:14.110
So let's go ahead and create our graph configuration.

49
03:15.730 --> 03:20.080
To start off, I have to specify an ID. And in this case,

50
03:20.110 --> 03:23.590
it has to start off with some letters from A to Z,

51
03:23.920 --> 03:26.050
and then it could be letters or numbers.

52
03:26.500 --> 03:29.680
And it has to be between 1 and 16 characters.

53
03:30.100 --> 03:33.280
Let's say that the id of my graph is going to be graph1,

54
03:33.730 --> 03:38.730
notice how we can't have the number just at the beginning because of these rules.

55
03:40.450 --> 03:42.460
Next is the name of the graph.

56
03:44.320 --> 03:46.750
So mine is going to be my cycling graph,

57
03:47.830 --> 03:50.110
and then it's the unit of measure.

58
03:50.860 --> 03:55.030
So because I'm tracking my cycling, it's going to be in kilometers.

59
03:55.960 --> 04:00.880
And then it's going to be the type of data that I'm going to be posting in this

60
04:00.880 --> 04:03.310
unit. Because it's kilometers,

61
04:03.490 --> 04:06.370
I'm probably going to have some numbers after the decimal point.

62
04:06.640 --> 04:09.730
So I'm going to keep it as a float rather than an int.

63
04:10.780 --> 04:13.450
And finally, we're going to pick a color.

64
04:14.080 --> 04:18.370
I'm going to pick purple, ajisai, which is hydrangea flower.

65
04:18.910 --> 04:21.670
And just to make sure I don't make a mistake,

66
04:21.700 --> 04:24.160
I'm going to copy that and paste it in.

67
04:25.570 --> 04:30.520
That's all of my required graph config. But right now,

68
04:31.060 --> 04:33.280
if I do the same thing as I did before

69
04:33.310 --> 04:37.660
which is to put in my graph endpoint as the URL

70
04:38.080 --> 04:42.460
and then the JSON is set to equal the graph config,

71
04:42.970 --> 04:47.440
if I actually post this right now, it's not going to work.

72
04:47.680 --> 04:52.570
And if I print the response.text, you'll see the reason.

73
04:54.610 --> 04:58.750
The message tells me that the user angela does not exist.

74
04:59.110 --> 05:03.790
I know it exists because I created in this step and I got a successful response,

75
05:04.990 --> 05:09.940
or it's telling me that the token is wrong. So think about it.

76
05:09.970 --> 05:12.760
Where have we actually provided our password?

77
05:12.760 --> 05:16.750
Where if we provided our token in this request? No where.

78
05:17.380 --> 05:19.930
It's not one of the request body keys.

79
05:20.440 --> 05:24.070
So how do we provide our authentication? Well,

80
05:24.070 --> 05:26.620
it's done using the request header.

81
05:27.130 --> 05:29.080
We have to provide this key

82
05:29.170 --> 05:34.170
and it's gonna look for the token that we used to register with Pixela.

83
05:35.650 --> 05:40.060
You might have even noticed previously when we were working with the news API

84
05:40.390 --> 05:42.820
that when we were looking at the authentication,

85
05:43.150 --> 05:48.150
even though there's three methods for authentication, via a API key, query string

86
05:49.330 --> 05:50.050
parameter

87
05:50.050 --> 05:55.050
which is what we used or via the X-Api-Key HTTP header or via the authorization

88
05:56.750 --> 05:57.680
HTEP header.

89
05:58.160 --> 06:02.810
And you can see that they actually say we strongly recommend either of the

90
06:02.810 --> 06:06.350
latter two, so using the HTTP header.

91
06:06.830 --> 06:11.270
And the reason is so that your API key isn't visible to others in logs

92
06:11.510 --> 06:13.730
or if they're doing requests sniffing.

93
06:14.990 --> 06:17.720
So what exactly are these headers anyways?

94
06:18.020 --> 06:21.290
Well if you think about a letter that you might get in the mail,

95
06:21.530 --> 06:26.390
the header is the part that contains some relevant pieces of information,

96
06:26.390 --> 06:27.350
like, um,

97
06:27.620 --> 06:32.240
the phone numbers of the company or their website of the company or their logo.

98
06:32.780 --> 06:36.380
And then the body is the actual message part sesentially.

99
06:36.380 --> 06:40.610
It's the part that changes from letter to letter. Now,

100
06:40.640 --> 06:45.640
back in the previous project where we created our stock news alert application,

101
06:46.970 --> 06:51.970
you can see that we're simply sending our API key as a parameter

102
06:53.810 --> 06:58.810
along with the get request. And what this effectively looks like

103
06:59.510 --> 07:04.220
if we're doing it in the browser is we're taking the news endpoint,

104
07:04.670 --> 07:09.670
we're putting it into the URL bar and then we're adding our parameters.

105
07:10.700 --> 07:12.890
So the first one is the API key.

106
07:14.540 --> 07:18.350
And then the next one is the actual query.

107
07:19.550 --> 07:23.990
So we were querying for the company name in the title of the news piece

108
07:24.440 --> 07:26.600
and we were looking for Tesla.

109
07:27.320 --> 07:31.460
This is effectively exactly the same as our code here.

110
07:31.970 --> 07:36.200
We make a request to that URL and pass over these parameters.

111
07:36.560 --> 07:39.770
There's effectively no difference between this and that.

112
07:40.370 --> 07:45.110
As you can imagine, all our secret stuff in the request itself.

113
07:45.500 --> 07:47.450
So if anybody's monitoring this,

114
07:47.510 --> 07:51.710
then they will be able to see all of this information and they might be able to

115
07:51.710 --> 07:54.890
steal your API key. Now,

116
07:54.890 --> 07:59.890
one of the things that's reassuring is that this API is accessed via HTTPS and

117
08:01.880 --> 08:04.070
the S stands for secure.

118
08:04.400 --> 08:07.610
So this request is actually encrypted,

119
08:07.970 --> 08:12.970
but it doesn't stop somebody from installing something on your browser to look

120
08:13.130 --> 08:14.660
at what requests you're making

121
08:15.260 --> 08:20.260
and there are certain cases where this might leak your API key to somebody that

122
08:21.530 --> 08:25.370
you don't want to. So for some API providers,

123
08:25.400 --> 08:29.630
you'll see that they want you to provide the authentication in the header.

124
08:30.410 --> 08:35.240
Let's go ahead and do that. So let's take the key that they're expecting

125
08:35.870 --> 08:39.410
and let's create a dictionary called headers,

126
08:41.660 --> 08:44.210
and let's make that the key,

127
08:44.540 --> 08:47.930
and then the value is going to be our token.

128
08:48.270 --> 08:51.950
This is our top secret password. Now,

129
08:51.950 --> 08:56.950
in addition to our URL on the JSON that we're sending over to Pixela,

130
08:57.390 --> 09:02.340
we're also going to add a optional keyword argument which is called headers.

131
09:02.910 --> 09:07.860
This is not going to show up when you start typing, not like JSON or URL.

132
09:08.010 --> 09:12.990
And this is because it's one of the kwargs or keyword arguments.

133
09:13.590 --> 09:17.370
And that means you also have to spell it exactly right

134
09:17.460 --> 09:21.750
and make sure you don't make any typos. It's headers with an S at the end.

135
09:22.260 --> 09:26.370
And then we're going to pass our variable headers into this parameter.

136
09:27.990 --> 09:31.350
Now what we're going to make our request once more,

137
09:31.830 --> 09:35.640
and you can see this time when we print the response.text,

138
09:35.730 --> 09:40.730
it gives us success and we've now managed to set up our brand new cycling graph.

139
09:42.890 --> 09:45.290
So if we go back to step three,

140
09:45.410 --> 09:50.410
we can actually go ahead and look at our cycling graph by going to this URL and

141
09:50.840 --> 09:55.550
replacing this part which is the creator's username with our own,

142
09:55.580 --> 09:58.490
so mine was angela. Yours will be something else.

143
09:58.790 --> 10:02.540
And we're also going to replace this final part with our ID,

144
10:02.720 --> 10:05.090
which in my case is just graph1.

145
10:05.870 --> 10:09.890
Now we're going to finally tag on .html to the end,

146
10:09.980 --> 10:11.120
and when I hit enter,

147
10:11.480 --> 10:16.480
you'll see it go to my brand new cycling graph right here.

148
10:17.150 --> 10:20.900
And it's going to be colored in purple and it's going to look beautiful and it's

149
10:20.900 --> 10:23.840
going to start tracking my cycling data.

150
10:25.070 --> 10:29.750
So this is how we can authenticate ourselves using a header

151
10:30.140 --> 10:35.140
and the requests module makes passing over headers just as easy as passing over

152
10:35.990 --> 10:40.990
other parameters or other parts of the message body. In the next lesson,

153
10:41.660 --> 10:46.100
we're going to be moving on to step 4 so that we can finally add a

154
10:46.130 --> 10:48.740
the pixel of data to our graph.

155
10:49.250 --> 10:51.830
So head over to the next lesson and give that a go.