Ask a Question

Auth Token is not getting refreshed due to which API is getting failed with error 401


Auth Token is not getting refreshed due to which API is getting failed with error 401

Hi @richie


Auth Token is not getting refreshed automatically though I have ticked the checbox in settings too

and due to which I have to intervene manually and update the token 

Please refer the screenshot for the same 


Hi @sonya_m  

I have automated the script till I get the token in server , I have attached the screenshot for the same , 

I am not that good in scripting so can you help how to get this token in use in the script 

@Jainsoumya Hi, I'm not really sure if this is what you're looking for, but maybe it'll come in handy, you can just create a new groovy script step before everything in order to refresh the token. Go here to the 'Forced token update' section and copy the script from there. Hope it helps you.

Occasional Contributor



we update our tokens via the SubmitListener - beforeSubmit-Event

Our groovy checks the status of the required token and updates if it is older than x minutes (because the automatic function not works)


Kind Regards, Michael



Michael Krausemann
Frequent Contributor

@Jainsoumya, looks like you are using OAuth (1 or 2) for your access token. 

Once you configure your authProfile to retrieve token, you can use event handlers to run your scripts to automatically update token.


We have implemented very similar approach. we are using TestRunListener.beforeStep and this script will be called only for a REST step (this is done using script logic). you also could do this for SubmitListener.beforeStep event too.



//## Get Project ##//
def project = testRunner.testCase.testSuite.getProject()


//## Get TestStep ##//
def TestStep = context.getCurrentStep()

//## Get oAuth Profile Name ##//
def oAuthProfileName
def oAuthProfile
def TokenStatus
def oAuthClientFacade
def currentToken
def ENV
def i = 0

if (TestStep.config.type == 'restrequest'){




//## get current environment ##// "Running GetAccessToken event script..."
ENV = "test environment is $ENV..."

//## Get oAuth profile name list ##//
def oAuthProfilelist = project.getOAuth2ProfileContainer().getOAuth2ProfileNameList()

oAuthProfileName = oAuthProfilelist[i]
// "oAuth Profile name is: $oAuthProfileName"

if(oAuthProfileName == "$ENV"){
// "oAuth Profile for $ENV is: $oAuthProfileName"


//## Get Token Status ##//
oAuthProfile = project.getAuthRepository().getEntry("$oAuthProfileName")
TokenStatus = oAuthProfile.accessTokenStatus.toString() "Access Token Status for $oAuthProfileName is: $TokenStatus"


//## Get Access Token from the server if expired ##//
if ((TokenStatus != 'RETRIEVED_FROM_SERVER') || (currentToken == oAuthProfile.getAccessToken())) { "Retrieving token from the auth server..."
oAuthClientFacade = new OltuOAuth2ClientFacade(TokenType.ACCESS)
oAuthClientFacade.requestAccessToken(oAuthProfile, true) "Access Token Status is " + oAuthProfile.accessTokenStatus




@New2API Hi

I am not sure where to write and use this code as token in only getting generated at browser , not able to capture it in token field 

I am not sure you are getting my issue or not . We can connect over a call if its ok with you 


Frequent Contributor

@Jainsoumya, probably you are right. I am not getting a full picture of your test configuration. However, from your Auth Manager snapshot it is clear that you are using an Authorization Profile to generate a token. Also, I am assuming that there is way to generate a token from Auth profile screen when it expires.


We architected our tests to use these three components of ReadyAPI  -  environment, AuthProfile and event handlers.   

Environments comes handy if you want to run tests in different environments say QA, Dev, or Pre-PROD etc. Also, you can have different auth profiles for each environment and switching between different environments makes it easy. Finally event handlers to perform certain task based on the conditions such as authToken refresh before a service call.


please refer to attached snapshot to get an idea about event handlers usage.



Please note that you may have to run the test at testcase level or testsuite level in order to see this specific even to trigger.

Hope this helps.

@New2API  Can we connect over a call . If its ok with you ?

Hi @richie @New2API @aabad 

Auth token is not getting populated in the token field please note Its and OIDC connect .

I have added all the required fields and I have added steps in automation script , but when I click on get OpenID token , token is not getting populated in the field mentioned in GetID token .

I am new to readyapi so I am not aware of groovy script too 

Please help 

@sonya_m  I didnt get any solution from the incident which I have created for the same Case #00508484: OAuth Token Automation





Frequent Contributor

@Jainsoumya, call is not a problem. but how to reach you? (do not want to put my email/ph# here 🙂 )

@New2API Check message 🙂

Showing results for 
Search instead for 
Did you mean: