WEBVTT

1
00:00.000 --> 00:05.000
Just like we did with the DNA sensor, we were ultimately able to migrate what

2
00:05.000 --> 00:05.760
we did in

3
00:05.760 --> 00:10.080
Postman and to Python, let's do the same thing with respect to SD1.

4
00:10.080 --> 00:14.240
So let me go out and create a new directory, let's just call this SD1.

5
00:14.240 --> 00:17.190
And inside of this directory, what I will do is I'll create a script called

6
00:17.190 --> 00:18.160
script2.py.

7
00:18.160 --> 00:22.080
All right, so let's go and open this up then.

8
00:22.080 --> 00:25.120
Now you already know the library that I'm going to be using, that's going to be

9
00:25.120 --> 00:25.360
the

10
00:25.360 --> 00:28.410
request library, so let's import it and let's try to type it the right way

11
00:28.410 --> 00:29.080
without matching

12
00:29.080 --> 00:30.080
the keypad.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

12
00:29.080 --> 00:30.080
the keypad.

13
00:30.080 --> 00:31.760
Okay, there we go, a little bit better.

14
00:31.760 --> 00:35.550
And to begin, just for posterity, what I will do is I will paste in my little

15
00:35.550 --> 00:36.360
configuration

16
00:36.360 --> 00:39.680
to stop any warnings coming up, keep things nice and neat.

17
00:39.680 --> 00:44.320
And once again, I'll specify my username, which to log in to DevNet Sandbox is

18
00:44.320 --> 00:44.760
going

19
00:44.760 --> 00:45.760
to be DevNet user.

20
00:45.760 --> 00:50.120
Let me put this with the invitation marks and I'll specify my password.

21
00:50.120 --> 00:51.120
Perfect.

22
00:51.120 --> 00:54.170
So remember, the very first thing that I'm going to have to do is to make a

23
00:54.170 --> 00:54.720
request to

24
00:54.720 --> 01:00.240
that end point, J_security_check, that will give me my session ID, my cookie.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

24
00:54.720 --> 01:00.240
that end point, J_security_check, that will give me my session ID, my cookie.

25
01:00.240 --> 01:04.560
And from that point on, we'll be able to get a CSRF token.

26
01:04.560 --> 01:08.160
So to begin, let's specify what the cookie URL will be.

27
01:08.160 --> 01:12.220
So I'll just call this cookie URL is equal to, now again, we go back and grab

28
01:12.220 --> 01:12.880
this address

29
01:12.880 --> 01:13.880
right here.

30
01:13.880 --> 01:17.000
The invitation marks, let's paste this in and remember, it's going to be

31
01:17.000 --> 01:17.680
forward slash

32
01:17.680 --> 01:20.240
J_security_check.

33
01:20.240 --> 01:26.410
Now, remember, with respect to the actual authentication, we're not going to

34
01:26.410 --> 01:27.120
specify

35
01:27.120 --> 01:30.480
basic authentication, just the regular username and password.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

35
01:27.120 --> 01:30.480
basic authentication, just the regular username and password.

36
01:30.480 --> 01:34.950
Instead, we're going to pass along the information and the body of the HTTP

37
01:34.950 --> 01:35.720
request.

38
01:35.720 --> 01:39.160
What I will do here is just say a body is equal to, and I will have a

39
01:39.160 --> 01:40.720
dictionary here.

40
01:40.720 --> 01:45.510
And the key is going to be J_ username and the corresponding value is going to

41
01:45.510 --> 01:46.360
be, well,

42
01:46.360 --> 01:48.760
it'll be the username that we just defined above.

43
01:48.760 --> 01:53.080
And I'll have a comma and the next key will be J_password.

44
01:53.080 --> 01:58.880
In fact, just to be clear here, let me just move that colon outside to here.

45
01:58.880 --> 02:00.560
That's a bit better.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

45
01:58.880 --> 02:00.560
That's a bit better.

46
02:00.560 --> 02:02.720
And again, specify the password.

47
02:02.720 --> 02:04.640
Now I'm also going to have to define my headers.

48
02:04.640 --> 02:08.360
So again, we'll have my calibrators for my dictionary.

49
02:08.360 --> 02:11.000
And I'll say the content type.

50
02:11.000 --> 02:14.720
This time it's going to be equal to application, forward slash.

51
02:14.720 --> 02:20.320
And remember this one, x www. form URL encoded, see it's just the same thing.

52
02:20.320 --> 02:21.800
It's just a different format.

53
02:21.800 --> 02:25.030
This time with Python, we type it a particular way, whereas with Postman, we

54
02:25.030 --> 02:25.640
enter the same

55
02:25.640 --> 02:28.720
information in a slightly different way via the graphical user interface.

56
02:28.720 --> 02:32.640
Ultimately though, the same important information remains J_ username, J_
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

56
02:28.720 --> 02:32.640
Ultimately though, the same important information remains J_ username, J_

57
02:32.640 --> 02:34.960
password, x www.form,

58
02:34.960 --> 02:37.160
URL encoded, all that stuff still applies.

59
02:37.160 --> 02:37.680
OK.

60
02:37.680 --> 02:40.200
Now let's go and send off this request.

61
02:40.200 --> 02:43.000
So let's just say, cookie it responds.

62
02:43.000 --> 02:47.510
It's equal to-- now remember, just as we saw in the Postman demonstration, we

63
02:47.510 --> 02:47.640
're going

64
02:47.640 --> 02:49.040
to use a Post request.

65
02:49.040 --> 02:52.160
And I'll say the URL is equal to the cookie URL.

66
02:52.160 --> 02:55.920
I'll say the data is equal to the body that I've just defined.

67
02:55.920 --> 02:58.960
And I'll say the headers are equal to the headers that I've just defined.

68
02:58.960 --> 03:01.980
The fact about what we'll do here is I'll just make this a little bit more
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

68
02:58.960 --> 03:01.980
The fact about what we'll do here is I'll just make this a little bit more

69
03:01.980 --> 03:02.520
explicit

70
03:02.520 --> 03:04.920
and just say cookie body.

71
03:04.920 --> 03:06.080
And cookie headers.

72
03:06.080 --> 03:09.520
So again, let's just change this cookie body and cookie headers.

73
03:09.520 --> 03:12.120
Now all I will do is I will use Rprints.

74
03:12.120 --> 03:18.280
So let's just say from Rich import Prints as Rprints.

75
03:18.280 --> 03:21.080
Now RPrint the cookie response.

76
03:21.080 --> 03:22.360
So let's save this.

77
03:22.360 --> 03:23.800
And let's try to run the script.

78
03:23.800 --> 03:25.840
It's 2.py and to enter.

79
03:25.840 --> 03:27.360
Oh, I've mistyped body.

80
03:27.360 --> 03:28.160
I wrote that twice.

81
03:28.160 --> 03:29.000
Pause a bit of that.

82
03:29.000 --> 03:30.600
Let's just fix that there.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

82
03:29.000 --> 03:30.600
Let's just fix that there.

83
03:30.600 --> 03:31.320
There we go.

84
03:31.320 --> 03:32.040
Try again.

85
03:32.040 --> 03:33.680
And I have another error.

86
03:33.680 --> 03:34.840
Can you spot where it is?

87
03:34.840 --> 03:38.360
What I'm going to have to do here is to specify that I don't want to verify my

88
03:38.360 --> 03:38.800
SSL

89
03:38.800 --> 03:39.360
certificates.

90
03:39.360 --> 03:40.160
I'll say verify.

91
03:40.160 --> 03:40.960
It's equal to false.

92
03:40.960 --> 03:42.360
Save you.

93
03:42.360 --> 03:43.480
Clear the screen.

94
03:43.480 --> 03:44.680
For time lucky.

95
03:44.680 --> 03:45.440
There we go right there.

96
03:45.440 --> 03:46.960
We get our 200 response.

97
03:46.960 --> 03:51.160
So what I will do to actually see the output of the cookie is I can get the dot

98
03:51.160 --> 03:51.560
cookies

99
03:51.560 --> 03:52.240
attribute here.

100
03:52.240 --> 03:53.920
OK, check this out.

101
03:53.920 --> 03:56.120
If I save this, rerun the script.

102
03:56.120 --> 03:58.960
And as we can see, we're now getting back our cookie response.

103
03:58.960 --> 04:01.280
We have our J session ID right here.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

103
03:58.960 --> 04:01.280
We have our J session ID right here.

104
04:01.280 --> 04:04.240
Let's grab that key to get the information.

105
04:04.240 --> 04:06.680
So say J session ID.

106
04:06.680 --> 04:07.600
Save you.

107
04:07.600 --> 04:08.640
Run this again.

108
04:08.640 --> 04:10.280
And this is the actual cookie value.

109
04:10.280 --> 04:13.320
So what I will do here is just store this as a variable.

110
04:13.320 --> 04:17.560
As opposed to printing, let's just save this as J session underscore cookie.

111
04:17.560 --> 04:18.640
OK, so there we go.

112
04:18.640 --> 04:22.160
So we can now reuse this as this particular variable value here.

113
04:22.160 --> 04:25.720
So now we're going to have to get our CSRF token.

114
04:25.720 --> 04:28.200
That's creating a URL called token URL.

115
04:28.200 --> 04:30.320
Let's just grab this right here.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

115
04:28.200 --> 04:30.320
Let's just grab this right here.

116
04:30.320 --> 04:31.160
Paste it in.

117
04:31.160 --> 04:34.360
And let's just modify the endpoint right here.

118
04:34.360 --> 04:37.910
This time it's going to be forward slash data service forward slash client

119
04:37.910 --> 04:38.600
forward slash

120
04:38.600 --> 04:40.000
token.

121
04:40.000 --> 04:43.600
And now I'm going to do is in my request, I'm going to have my cookies here.

122
04:43.600 --> 04:44.960
So let's create a little dictionary.

123
04:44.960 --> 04:47.800
And the key will be J session ID.

124
04:47.800 --> 04:51.760
And that's going to be equal to the J session cookie that I just parsed out

125
04:51.760 --> 04:52.360
right here.

126
04:52.360 --> 04:56.320
So let's create a variable called token response.

127
04:56.320 --> 04:57.360
Use a request library.

128
04:57.360 --> 04:58.920
This time it's going to be a get request.

129
04:58.920 --> 05:01.880
The URL will be the token URL this time.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

129
04:58.920 --> 05:01.880
The URL will be the token URL this time.

130
05:01.880 --> 05:05.060
And I'll say the cookies that I want to specify are going to be equal to the

131
05:05.060 --> 05:05.560
cookies that

132
05:05.560 --> 05:08.120
I've just defined.

133
05:08.120 --> 05:10.720
And again, I'll say verify is equal to false.

134
05:10.720 --> 05:13.770
And then all I will do is I will print the response and I'll take the text

135
05:13.770 --> 05:14.360
attribute to

136
05:14.360 --> 05:16.560
see what we actually get back.

137
05:16.560 --> 05:17.720
So run the script.

138
05:17.720 --> 05:20.840
And now we can see here this is the CSRF token.

139
05:20.840 --> 05:24.480
So once again, save it into a variable for future use.

140
05:24.480 --> 05:30.400
CSRF underscore token is equal to this particular value.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

140
05:24.480 --> 05:30.400
CSRF underscore token is equal to this particular value.

141
05:30.400 --> 05:33.560
And now we can begin creating request to our endpoints.

142
05:33.560 --> 05:38.560
Let's just say endpoint URL is equal to again, let's just grab this URL for a

143
05:38.560 --> 05:39.040
copy.

144
05:39.040 --> 05:44.000
And we'll just use the same URL as we did in postman forward slash data service

145
05:44.000 --> 05:44.200
forward

146
05:44.200 --> 05:45.200
slash device.

147
05:45.200 --> 05:47.280
Again, you can choose any which one you saw.

148
05:47.280 --> 05:50.000
I'll create my end point headers.

149
05:50.000 --> 05:55.220
And I'll say x xsRF, I've been talking, is equal to the CSRF token I've just

150
05:55.220 --> 05:55.800
parsed

151
05:55.800 --> 05:56.800
out.

152
05:56.800 --> 06:00.000
And now what I will do is I'll create my end point response.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

153
06:00.000 --> 06:03.850
So to the request library with a get request, the URL this time is going to be

154
06:03.850 --> 06:04.640
the endpoint

155
06:04.640 --> 06:05.640
URL.

156
06:05.640 --> 06:10.200
So the headers are equal to the endpoint headers to include the CSRF token.

157
06:10.200 --> 06:13.720
Also, the cookies are equal to the cookies I've defined above.

158
06:13.720 --> 06:16.760
And once again, verify will be set to false.

159
06:16.760 --> 06:19.800
And finally, I'll just our print the endpoint response.

160
06:19.800 --> 06:23.200
If I save this, run the script.

161
06:23.200 --> 06:25.040
Here we have a 200 status code.

162
06:25.040 --> 06:26.760
Let's get the actual JSON object.

163
06:26.760 --> 06:28.360
So let's call the JSON method.

164
06:28.360 --> 06:30.520
Let's go to the screen one more time.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

164
06:28.360 --> 06:30.520
Let's go to the screen one more time.

165
06:30.520 --> 06:34.250
And if we hit enter, here we can see here we have all the list of the devices

166
06:34.250 --> 06:34.760
within

167
06:34.760 --> 06:36.320
SD1 once more.

168
06:36.320 --> 06:39.770
See that right here, device ID, hostname, reachability, all of that stuff now

169
06:39.770 --> 06:41.320
available to us programmatically

170
06:41.320 --> 06:44.480
within Python once again, the same thing to remain.

171
06:44.480 --> 06:48.700
We take our username and password target this particular endpoint of J security

172
06:48.700 --> 06:49.600
check, specify

173
06:49.600 --> 06:51.640
the content type right here.

174
06:51.640 --> 06:55.590
And then we send off that post request, which ultimately allows us to get our J

175
06:55.590 --> 06:56.400
session ID

176
06:56.400 --> 06:57.400
cookie.

177
06:57.400 --> 07:01.280
If this were able to now target the token endpoint, which we use via a GET
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

177
06:57.400 --> 07:01.280
If this were able to now target the token endpoint, which we use via a GET

178
07:01.280 --> 07:02.320
request, we parse

179
07:02.320 --> 07:05.240
out that particular cookie and then pass it into this request.

180
07:05.240 --> 07:08.760
This allows us to get our cross site request for Jory token.

181
07:08.760 --> 07:12.240
And then we can interact with all of the API endpoints with GET request, post

182
07:12.240 --> 07:12.760
request,

183
07:12.760 --> 07:17.090
delete request, whatever it may be by passing in that token along with the

184
07:17.090 --> 07:17.920
header key,

185
07:17.920 --> 07:19.960
x, xsRF token.

186
07:19.960 --> 07:22.520
And now we have access to all of our data programmatically.
