Forum Discussion
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
- New2API3 years agoFrequent 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.
- Jainsoumya3 years agoContributor
New2API Can we connect over a call . If its ok with you ?
- Jainsoumya3 years agoContributor
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
- New2API3 years agoFrequent Contributor
Jainsoumya, call is not a problem. but how to reach you? (do not want to put my email/ph# here 🙂 )
- Jainsoumya3 years agoContributor
New2API Check message 🙂