Dynamic Access Token and update to HTTP Header

Hiya,

I’m kind of stuck using the API with Katalon.
I’m using an OAtuh 2.0 type and I have set up everything correctly… And it does work.
However the Access Token changes every day. That means I have to manually request token and update to HTTP Header everyday on all my API Objects.

I imagine this can be done in a script, however I have no clue how and I’m not finding the solution on the internet with my knowledge of coding.

Is there anyone in the community that may point me into the right direction?

Ideally I want my script to Request token and then Update to HTTP Header, before running the GET.

Ok, I found the solution myself.
For anyone that wants the solution.

loginPostLuna = WS.sendRequest(findTestObject(‘API/loginPostLuna’))
def slurper = new JsonSlurper()
Map accessToken = slurper.parseText(loginPostLuna.getResponseText())
GlobalVariable.accessToken = accessToken.access_token
println(GlobalVariable.accessToken)
response = WS.sendRequest(findTestObject(‘API/getUserProfile’))
WS.verifyResponseStatusCode(response, 200)
image
image

Basically what I am doing is;
first I store the Object which gives the access Token, into a variable loginPostLuna

Then I use the JsonSlurper and put the text from my loginPostLuna variable into accessToken variable.

I then store the string which is in the accessToken into a Global Variable.

I use the println to see the value of the GlobalVariable (actually used this for troubleshooting).

The global variable are called upon in the Object getUserProfile (Both in Access Token and HTTP Header).

I then store the response from my getUserProfile into a variable and check the statuscode if it’s 200 (or whatever number you want it to be).

Hope this may help anyone that had the same issue I was having.

2 Likes