Ask a Question

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

Jainsoumya
Contributor

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 

19 REPLIES 19

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.

mkrausem
Occasional Contributor

Hi,

 

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
New2API
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 ##//
log.info "Running GetAccessToken event script..."
ENV = testRunner.testCase.testSuite.project.activeEnvironment.name
log.info "test environment is $ENV..."


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

oAuthProfilelist.each{
oAuthProfileName = oAuthProfilelist[i]
//log.info "oAuth Profile name is: $oAuthProfileName"

if(oAuthProfileName == "$ENV"){
//log.info "oAuth Profile for $ENV is: $oAuthProfileName"
return
}
i++

}

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

//oAuthProfile.setClientID("$ClientID")
//oAuthProfile.setClientSecret("$ClientSecret")

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

}

 

thanks.

@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 

 

New2API
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.

 

ReadyAPI-EventHandlers.png

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

Jainsoumya_1-1646376110785.png

 

 

 

New2API
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 🙂

cancel
Showing results for 
Search instead for 
Did you mean: