1
00:00:00,360 --> 00:00:03,090
Previously we completed step one

2
00:00:03,330 --> 00:00:06,960
which is setting up a user account on Pixela.

3
00:00:07,590 --> 00:00:09,360
Now we can move on to step two

4
00:00:09,450 --> 00:00:13,920
which is to create a new graph on Pixela for our username.

5
00:00:14,550 --> 00:00:19,550
So notice how the endpoint is now different because in addition to /

6
00:00:20,160 --> 00:00:21,210
v1/users

7
00:00:21,480 --> 00:00:26,480
we also have to add our username now and then for /graphs.

8
00:00:27,030 --> 00:00:31,590
So let's go back to the documentation and scroll down to the relevant part of

9
00:00:31,590 --> 00:00:36,030
the API, which is this part to create a graph, that's step two.

10
00:00:37,410 --> 00:00:40,890
This is also going to be a HTTP post request,

11
00:00:41,310 --> 00:00:43,770
but the end point has now changed.

12
00:00:44,280 --> 00:00:47,160
So let's go ahead and create our new endpoint

13
00:00:47,580 --> 00:00:49,770
which is the graph endpoint.

14
00:00:51,750 --> 00:00:56,340
And this is going to be comprised of the Pixela endpoint

15
00:00:56,700 --> 00:01:00,780
and then in addition, it's going to be / our username.

16
00:01:01,770 --> 00:01:03,090
So let's take out our

17
00:01:03,090 --> 00:01:07,050
username and our token and put it as a constant at the top.

18
00:01:07,130 --> 00:01:07,963
Right

19
00:01:14,840 --> 00:01:17,720
Now we can add that in here using an f-string

20
00:01:20,630 --> 00:01:23,300
and then finally it's /graphs.

21
00:01:25,700 --> 00:01:27,920
Now, remember if you're doing this along with me,

22
00:01:27,950 --> 00:01:32,750
this username is going to have to be your own unique username and the token is

23
00:01:32,750 --> 00:01:34,700
going to be your own secret token.

24
00:01:36,230 --> 00:01:38,360
Once we've created our graph end point,

25
00:01:38,600 --> 00:01:43,130
the next step is to make our post request. Again,

26
00:01:43,160 --> 00:01:48,160
we're going to use our request module and call the post method to make this

27
00:01:48,200 --> 00:01:52,760
request. This time what is the data that we want to send over?

28
00:01:53,330 --> 00:01:53,690
Well,

29
00:01:53,690 --> 00:01:58,690
we want to be able to add the ID of our new graph,

30
00:01:59,300 --> 00:02:04,300
so some way of identifying it in the URL, and then the name of the graph,

31
00:02:05,090 --> 00:02:09,590
and then the unit of measure that we're keeping track of. For example,

32
00:02:09,590 --> 00:02:11,600
if I'm keeping track of my cycling,

33
00:02:11,840 --> 00:02:14,540
then that's going to be kilometers that I've cycled,

34
00:02:14,870 --> 00:02:18,950
or if I want to keep track of how long I'm coding

35
00:02:18,980 --> 00:02:21,320
then that might be minutes or hours.

36
00:02:21,950 --> 00:02:25,850
And then the data type of the unit of measure.

37
00:02:26,240 --> 00:02:29,930
If I was tracking the number of pages I've read then they'll be whole numbers,

38
00:02:30,320 --> 00:02:32,660
if I'm tracking the number of kilometers I've ridden

39
00:02:32,900 --> 00:02:36,470
then it's probably going to be a floating point number. And then finally,

40
00:02:36,500 --> 00:02:39,530
we're going to specify the color of our graph. Here

41
00:02:39,530 --> 00:02:42,950
you have to be a little bit careful because these are strings

42
00:02:42,980 --> 00:02:46,160
which are actually in Japanese. So for example,

43
00:02:46,580 --> 00:02:48,680
momiji is autumn leaves.

44
00:02:49,250 --> 00:02:52,580
shibafu is grass, sara is sky,

45
00:02:53,300 --> 00:02:58,300
and they've got a whole bunch of poetic names for each of these colors,

46
00:02:58,790 --> 00:03:01,120
which wonderful, but it also prone to errors.

47
00:03:01,180 --> 00:03:05,590
So you have to be careful when you're typing in the color that you want.

48
00:03:05,800 --> 00:03:09,640
And if in doubt, just simply copy it from the documentation.

49
00:03:10,930 --> 00:03:14,110
So let's go ahead and create our graph configuration.

50
00:03:15,730 --> 00:03:20,080
To start off, I have to specify an ID. And in this case,

51
00:03:20,110 --> 00:03:23,590
it has to start off with some letters from A to Z,

52
00:03:23,920 --> 00:03:26,050
and then it could be letters or numbers.

53
00:03:26,500 --> 00:03:29,680
And it has to be between 1 and 16 characters.

54
00:03:30,100 --> 00:03:33,280
Let's say that the id of my graph is going to be graph1,

55
00:03:33,730 --> 00:03:38,730
notice how we can't have the number just at the beginning because of these rules.

56
00:03:40,450 --> 00:03:42,460
Next is the name of the graph.

57
00:03:44,320 --> 00:03:46,750
So mine is going to be my cycling graph,

58
00:03:47,830 --> 00:03:50,110
and then it's the unit of measure.

59
00:03:50,860 --> 00:03:55,030
So because I'm tracking my cycling, it's going to be in kilometers.

60
00:03:55,960 --> 00:04:00,880
And then it's going to be the type of data that I'm going to be posting in this

61
00:04:00,880 --> 00:04:03,310
unit. Because it's kilometers,

62
00:04:03,490 --> 00:04:06,370
I'm probably going to have some numbers after the decimal point.

63
00:04:06,640 --> 00:04:09,730
So I'm going to keep it as a float rather than an int.

64
00:04:10,780 --> 00:04:13,450
And finally, we're going to pick a color.

65
00:04:14,080 --> 00:04:18,370
I'm going to pick purple, ajisai, which is hydrangea flower.

66
00:04:18,910 --> 00:04:21,670
And just to make sure I don't make a mistake,

67
00:04:21,700 --> 00:04:24,160
I'm going to copy that and paste it in.

68
00:04:25,570 --> 00:04:30,520
That's all of my required graph config. But right now,

69
00:04:31,060 --> 00:04:33,280
if I do the same thing as I did before

70
00:04:33,310 --> 00:04:37,660
which is to put in my graph endpoint as the URL

71
00:04:38,080 --> 00:04:42,460
and then the JSON is set to equal the graph config,

72
00:04:42,970 --> 00:04:47,440
if I actually post this right now, it's not going to work.

73
00:04:47,680 --> 00:04:52,570
And if I print the response.text, you'll see the reason.

74
00:04:54,610 --> 00:04:58,750
The message tells me that the user angela does not exist.

75
00:04:59,110 --> 00:05:03,790
I know it exists because I created in this step and I got a successful response,

76
00:05:04,990 --> 00:05:09,940
or it's telling me that the token is wrong. So think about it.

77
00:05:09,970 --> 00:05:12,760
Where have we actually provided our password?

78
00:05:12,760 --> 00:05:16,750
Where if we provided our token in this request? No where.

79
00:05:17,380 --> 00:05:19,930
It's not one of the request body keys.

80
00:05:20,440 --> 00:05:24,070
So how do we provide our authentication? Well,

81
00:05:24,070 --> 00:05:26,620
it's done using the request header.

82
00:05:27,130 --> 00:05:29,080
We have to provide this key

83
00:05:29,170 --> 00:05:34,170
and it's gonna look for the token that we used to register with Pixela.

84
00:05:35,650 --> 00:05:40,060
You might have even noticed previously when we were working with the news API

85
00:05:40,390 --> 00:05:42,820
that when we were looking at the authentication,

86
00:05:43,150 --> 00:05:48,150
even though there's three methods for authentication, via a API key, query string

87
00:05:49,330 --> 00:05:50,050
parameter

88
00:05:50,050 --> 00:05:55,050
which is what we used or via the X-Api-Key HTTP header or via the authorization

89
00:05:56,750 --> 00:05:57,680
HTEP header.

90
00:05:58,160 --> 00:06:02,810
And you can see that they actually say we strongly recommend either of the

91
00:06:02,810 --> 00:06:06,350
latter two, so using the HTTP header.

92
00:06:06,830 --> 00:06:11,270
And the reason is so that your API key isn't visible to others in logs

93
00:06:11,510 --> 00:06:13,730
or if they're doing requests sniffing.

94
00:06:14,990 --> 00:06:17,720
So what exactly are these headers anyways?

95
00:06:18,020 --> 00:06:21,290
Well if you think about a letter that you might get in the mail,

96
00:06:21,530 --> 00:06:26,390
the header is the part that contains some relevant pieces of information,

97
00:06:26,390 --> 00:06:27,350
like, um,

98
00:06:27,620 --> 00:06:32,240
the phone numbers of the company or their website of the company or their logo.

99
00:06:32,780 --> 00:06:36,380
And then the body is the actual message part sesentially.

100
00:06:36,380 --> 00:06:40,610
It's the part that changes from letter to letter. Now,

101
00:06:40,640 --> 00:06:45,640
back in the previous project where we created our stock news alert application,

102
00:06:46,970 --> 00:06:51,970
you can see that we're simply sending our API key as a parameter

103
00:06:53,810 --> 00:06:58,810
along with the get request. And what this effectively looks like

104
00:06:59,510 --> 00:07:04,220
if we're doing it in the browser is we're taking the news endpoint,

105
00:07:04,670 --> 00:07:09,670
we're putting it into the URL bar and then we're adding our parameters.

106
00:07:10,700 --> 00:07:12,890
So the first one is the API key.

107
00:07:14,540 --> 00:07:18,350
And then the next one is the actual query.

108
00:07:19,550 --> 00:07:23,990
So we were querying for the company name in the title of the news piece

109
00:07:24,440 --> 00:07:26,600
and we were looking for Tesla.

110
00:07:27,320 --> 00:07:31,460
This is effectively exactly the same as our code here.

111
00:07:31,970 --> 00:07:36,200
We make a request to that URL and pass over these parameters.

112
00:07:36,560 --> 00:07:39,770
There's effectively no difference between this and that.

113
00:07:40,370 --> 00:07:45,110
As you can imagine, all our secret stuff in the request itself.

114
00:07:45,500 --> 00:07:47,450
So if anybody's monitoring this,

115
00:07:47,510 --> 00:07:51,710
then they will be able to see all of this information and they might be able to

116
00:07:51,710 --> 00:07:54,890
steal your API key. Now,

117
00:07:54,890 --> 00:07:59,890
one of the things that's reassuring is that this API is accessed via HTTPS and

118
00:08:01,880 --> 00:08:04,070
the S stands for secure.

119
00:08:04,400 --> 00:08:07,610
So this request is actually encrypted,

120
00:08:07,970 --> 00:08:12,970
but it doesn't stop somebody from installing something on your browser to look

121
00:08:13,130 --> 00:08:14,660
at what requests you're making

122
00:08:15,260 --> 00:08:20,260
and there are certain cases where this might leak your API key to somebody that

123
00:08:21,530 --> 00:08:25,370
you don't want to. So for some API providers,

124
00:08:25,400 --> 00:08:29,630
you'll see that they want you to provide the authentication in the header.

125
00:08:30,410 --> 00:08:35,240
Let's go ahead and do that. So let's take the key that they're expecting

126
00:08:35,870 --> 00:08:39,409
and let's create a dictionary called headers,

127
00:08:41,659 --> 00:08:44,210
and let's make that the key,

128
00:08:44,540 --> 00:08:47,930
and then the value is going to be our token.

129
00:08:48,270 --> 00:08:51,950
This is our top secret password. Now,

130
00:08:51,950 --> 00:08:56,950
in addition to our URL on the JSON that we're sending over to Pixela,

131
00:08:57,390 --> 00:09:02,340
we're also going to add a optional keyword argument which is called headers.

132
00:09:02,910 --> 00:09:07,860
This is not going to show up when you start typing, not like JSON or URL.

133
00:09:08,010 --> 00:09:12,990
And this is because it's one of the kwargs or keyword arguments.

134
00:09:13,590 --> 00:09:17,370
And that means you also have to spell it exactly right

135
00:09:17,460 --> 00:09:21,750
and make sure you don't make any typos. It's headers with an S at the end.

136
00:09:22,260 --> 00:09:26,370
And then we're going to pass our variable headers into this parameter.

137
00:09:27,990 --> 00:09:31,350
Now what we're going to make our request once more,

138
00:09:31,830 --> 00:09:35,640
and you can see this time when we print the response.text,

139
00:09:35,730 --> 00:09:40,730
it gives us success and we've now managed to set up our brand new cycling graph.

140
00:09:42,890 --> 00:09:45,290
So if we go back to step three,

141
00:09:45,410 --> 00:09:50,410
we can actually go ahead and look at our cycling graph by going to this URL and

142
00:09:50,840 --> 00:09:55,550
replacing this part which is the creator's username with our own,

143
00:09:55,580 --> 00:09:58,490
so mine was angela. Yours will be something else.

144
00:09:58,790 --> 00:10:02,540
And we're also going to replace this final part with our ID,

145
00:10:02,720 --> 00:10:05,090
which in my case is just graph1.

146
00:10:05,870 --> 00:10:09,890
Now we're going to finally tag on .html to the end,

147
00:10:09,980 --> 00:10:11,120
and when I hit enter,

148
00:10:11,480 --> 00:10:16,480
you'll see it go to my brand new cycling graph right here.

149
00:10:17,150 --> 00:10:20,900
And it's going to be colored in purple and it's going to look beautiful and it's

150
00:10:20,900 --> 00:10:23,840
going to start tracking my cycling data.

151
00:10:25,070 --> 00:10:29,750
So this is how we can authenticate ourselves using a header

152
00:10:30,140 --> 00:10:35,140
and the requests module makes passing over headers just as easy as passing over

153
00:10:35,990 --> 00:10:40,990
other parameters or other parts of the message body. In the next lesson,

154
00:10:41,660 --> 00:10:46,100
we're going to be moving on to step 4 so that we can finally add a

155
00:10:46,130 --> 00:10:48,740
the pixel of data to our graph.

156
00:10:49,250 --> 00:10:51,830
So head over to the next lesson and give that a go.

