Just like we did with the DNA sensor, we were ultimately able to migrate what
we did in
Postman and to Python, let's do the same thing with respect to SD1.
So let me go out and create a new directory, let's just call this SD1.
And inside of this directory, what I will do is I'll create a script called
script2.py.
All right, so let's go and open this up then.
Now you already know the library that I'm going to be using, that's going to be
the
request library, so let's import it and let's try to type it the right way
without matching
the keypad. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
the keypad.
Okay, there we go, a little bit better.
And to begin, just for posterity, what I will do is I will paste in my little
configuration
to stop any warnings coming up, keep things nice and neat.
And once again, I'll specify my username, which to log in to DevNet Sandbox is
going
to be DevNet user.
Let me put this with the invitation marks and I'll specify my password.
Perfect.
So remember, the very first thing that I'm going to have to do is to make a
request to
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
that end point, J_security_check, that will give me my session ID, my cookie.
And from that point on, we'll be able to get a CSRF token.
So to begin, let's specify what the cookie URL will be.
So I'll just call this cookie URL is equal to, now again, we go back and grab
this address
right here.
The invitation marks, let's paste this in and remember, it's going to be
forward slash
J_security_check.
Now, remember, with respect to the actual authentication, we're not going to
specify
basic authentication, just the regular username and password. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
basic authentication, just the regular username and password.
Instead, we're going to pass along the information and the body of the HTTP
request.
What I will do here is just say a body is equal to, and I will have a
dictionary here.
And the key is going to be J_ username and the corresponding value is going to
be, well,
it'll be the username that we just defined above.
And I'll have a comma and the next key will be J_password.
In fact, just to be clear here, let me just move that colon outside to here.
That's a bit better. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
That's a bit better.
And again, specify the password.
Now I'm also going to have to define my headers.
So again, we'll have my calibrators for my dictionary.
And I'll say the content type.
This time it's going to be equal to application, forward slash.
And remember this one, x www. form URL encoded, see it's just the same thing.
It's just a different format.
This time with Python, we type it a particular way, whereas with Postman, we
enter the same
information in a slightly different way via the graphical user interface.
Ultimately though, the same important information remains J_ username, J_ WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Ultimately though, the same important information remains J_ username, J_
password, x www.form,
URL encoded, all that stuff still applies.
OK.
Now let's go and send off this request.
So let's just say, cookie it responds.
It's equal to-- now remember, just as we saw in the Postman demonstration, we
're going
to use a Post request.
And I'll say the URL is equal to the cookie URL.
I'll say the data is equal to the body that I've just defined.
And I'll say the headers are equal to the headers that I've just defined.
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
The fact about what we'll do here is I'll just make this a little bit more
explicit
and just say cookie body.
And cookie headers.
So again, let's just change this cookie body and cookie headers.
Now all I will do is I will use Rprints.
So let's just say from Rich import Prints as Rprints.
Now RPrint the cookie response.
So let's save this.
And let's try to run the script.
It's 2.py and to enter.
Oh, I've mistyped body.
I wrote that twice.
Pause a bit of that.
Let's just fix that there. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Let's just fix that there.
There we go.
Try again.
And I have another error.
Can you spot where it is?
What I'm going to have to do here is to specify that I don't want to verify my
SSL
certificates.
I'll say verify.
It's equal to false.
Save you.
Clear the screen.
For time lucky.
There we go right there.
We get our 200 response.
So what I will do to actually see the output of the cookie is I can get the dot
cookies
attribute here.
OK, check this out.
If I save this, rerun the script.
And as we can see, we're now getting back our cookie response.
We have our J session ID right here. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
We have our J session ID right here.
Let's grab that key to get the information.
So say J session ID.
Save you.
Run this again.
And this is the actual cookie value.
So what I will do here is just store this as a variable.
As opposed to printing, let's just save this as J session underscore cookie.
OK, so there we go.
So we can now reuse this as this particular variable value here.
So now we're going to have to get our CSRF token.
That's creating a URL called token URL.
Let's just grab this right here. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Let's just grab this right here.
Paste it in.
And let's just modify the endpoint right here.
This time it's going to be forward slash data service forward slash client
forward slash
token.
And now I'm going to do is in my request, I'm going to have my cookies here.
So let's create a little dictionary.
And the key will be J session ID.
And that's going to be equal to the J session cookie that I just parsed out
right here.
So let's create a variable called token response.
Use a request library.
This time it's going to be a get request.
The URL will be the token URL this time. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
The URL will be the token URL this time.
And I'll say the cookies that I want to specify are going to be equal to the
cookies that
I've just defined.
And again, I'll say verify is equal to false.
And then all I will do is I will print the response and I'll take the text
attribute to
see what we actually get back.
So run the script.
And now we can see here this is the CSRF token.
So once again, save it into a variable for future use.
CSRF underscore token is equal to this particular value. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
CSRF underscore token is equal to this particular value.
And now we can begin creating request to our endpoints.
Let's just say endpoint URL is equal to again, let's just grab this URL for a
copy.
And we'll just use the same URL as we did in postman forward slash data service
forward
slash device.
Again, you can choose any which one you saw.
I'll create my end point headers.
And I'll say x xsRF, I've been talking, is equal to the CSRF token I've just
parsed
out.
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
So to the request library with a get request, the URL this time is going to be
the endpoint
URL.
So the headers are equal to the endpoint headers to include the CSRF token.
Also, the cookies are equal to the cookies I've defined above.
And once again, verify will be set to false.
And finally, I'll just our print the endpoint response.
If I save this, run the script.
Here we have a 200 status code.
Let's get the actual JSON object.
So let's call the JSON method.
Let's go to the screen one more time. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Let's go to the screen one more time.
And if we hit enter, here we can see here we have all the list of the devices
within
SD1 once more.
See that right here, device ID, hostname, reachability, all of that stuff now
available to us programmatically
within Python once again, the same thing to remain.
We take our username and password target this particular endpoint of J security
check, specify
the content type right here.
And then we send off that post request, which ultimately allows us to get our J
session ID
cookie.
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
If this were able to now target the token endpoint, which we use via a GET
request, we parse
out that particular cookie and then pass it into this request.
This allows us to get our cross site request for Jory token.
And then we can interact with all of the API endpoints with GET request, post
request,
delete request, whatever it may be by passing in that token along with the
header key,
x, xsRF token.
And now we have access to all of our data programmatically.