Ask a Question

how to automate our manual Oauth2.0 process. ?

SOLVED
Preerab
Occasional Contributor

how to automate our manual Oauth2.0 process. ?

READY API 2.6.0

I am trying to automate token retrival part of my REST URI call. I am successful in generating OAUTH2.0 (client crential) token manually 'Get Token'

I am following Javascript and Groovy scripts 

https://support.smartbear.com/readyapi/docs/projects/requests/auth/types/oauth2/automate/sample.html

 

Under Page 1: I got Java script for Authorization.

try adding consent screen in Page 2

I tried Groovy script in Page 3 (whcih suppose to retrive token if its expired) 

Getting below error on Page 3

----------------------------------------------------------------------------------

 


The following script is invalid: // Import the required classes import com.eviware.soapui.impl.rest.actions.oauth.OltuOAuth2ClientFacade; import com.eviware.soapui.support.editor.inspectors.auth.TokenType; import com.eviware.soapui.model.support.ModelSupport; // Set up variables def project = ModelSupport.getModelItemProject(context.getModelItem()); def authProfile = project.getAuthRepository().getEntry("OAuth 2"); def oldToken = authProfile.getAccessToken(); def tokenType = TokenType.ACCESS; // Create a facade object def oAuthFacade = new OltuOAuth2ClientFacade(tokenType); // Request an access token in headless mode oAuthFacade.requestAccessToken(authProfile, true, true); // Wait until the access token gets updated while(oldToken == authProfile.getAccessToken()) { } //The sleep method can be used instead of a while loop //sleep(3000); // Post the info to the log log.info("Set new token: " + authProfile.getAccessToken()); Error: missing ; before statement (scriptToValidate#2)

--------------------------------------------------------------------------------------------------------------------

#

 

Attached screen shot.

I am sure, something silly I am missing. Appreciate help

 

8 REPLIES 8
jsheph01
Contributor

I have never used auth manager scripts but it looks like they are for creating a new token, not updating an existing one. The documentation you linked actually suggests that you use an event handler to update an existing token. You should be able to use that script with the event handler.

 

If you need to update multiple tokens, you might want to check this out.

https://community.smartbear.com/t5/SoapUI-Pro/Auto-Token-Generation-for-OAuth2-0/m-p/141490/highligh...

Preerab
Occasional Contributor

Thanks for reply,I tried adding 'TestRunListener.beforeRun' event and got below error

  • ERROR: Exception in request: java.lang.RuntimeException: Unable to refresh expired access token.
  • ERROR: An error occurred [Unable to refresh expired access token.], see error log for details
  • INFO: Error getting response for [http://my-service-dev.apps.proj.com.ProcessActionWithData:Request 1]; java.lang.RuntimeException: Unable to refresh expired access token.

AutoRefresh is not set on token, so I need to create new token if its expired.

Any handly code would be great.

 

Thanks,

TanyaYatskovska
SmartBear Alumni (Retired)

Hi @Preerab,

 

Could you please check if the solution posted here help you?

https://community.smartbear.com/t5/SoapUI-Open-Source/SOAPUI-auth2-accessToken-automatic-refresh/td-...

 

@nmrao, @Radford, @HimanshuTayal, @groovyguy, more ideas?

---------
Tanya Yatskovskaya
SmartBear Community and Education Manager



Preerab
Occasional Contributor

I have gone through above board. I have OAuth 2 'Client Credentials Grant'. So I can add the script above the  <con:clientID>

as  '<con:javaScripts>
<con:entry/>
<con:entry/>
</con:javaScripts>'.

But my questio as follows,

1) adip01 : dosent mention what kind of script is in place and where it is

2) I tried having 'Automation' and script as givien in https://support.smartbear.com/readyapi/docs/projects/requests/auth/types/oauth2/automate/sample.html

 

NOTE: Token auto refresh is not turned on, So requirement is to get new token on expiration.

No luck, any help is appreciated.

Nastya_Khovrina
SmartBear Alumni (Retired)

Hi @Preerab,

 

Can you please share the full code which you use on Page 3?  

 

Automating Token Retrieval on Request

By default, ReadyAPI does not check if a token has expired even if automated retrieval is enabled. You can use Groovy scripts to check if a token is up-to-date and retrieve a new token, if necessary. For this purpose, call the requestAccessToken method and specify the consoleMode parameter as described in the Automating Token Retrieval Example topic.

 

Do you use this method?


Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️

We use this script and it works so far. I didn't write it, so I can't any answer any question, but maybe it helps

 

We added it as Event SubmitListener.before Submit

 

// Import the required classes
import com.eviware.soapui.impl.rest.actions.oauth.OltuOAuth2ClientFacade
import com.eviware.soapui.support.editor.inspectors.auth.TokenType
import com.eviware.soapui.model.support.ModelSupport

def authProfileName = "PROFILE_NAME"

if(!submit.getRequest().getAuthType().asBoolean()){
    return // stop if the auth type is null, for example jdbc requests
}else if(submit.getRequest().getActiveAuthProfile() == null){
    return // stop if the auth profile is null
}else if(authProfileName == submit.getRequest().getActiveAuthProfile().getName()){

    // Set up variables
    def project = ModelSupport.getModelItemProject(context.getModelItem())
    def authProfile = project.getAuthRepository().getEntry(authProfileName)
    def oldToken = authProfile.getAccessToken()
    def tokenType = TokenType.ACCESS

    // Create a facade object
    def oAuthFacade = new OltuOAuth2ClientFacade(tokenType)

    // Request an access token in headless mode
    oAuthFacade.requestAccessToken(authProfile, true, true)

    // Wait until the access token gets updated
    //while(oldToken == authProfile.getAccessToken()) {}

    //The sleep method can be used instead of a while loop
    //sleep(3000)

    for(int i = 0; i<=3000; i++){
        if(oldToken != authProfile.getAccessToken()){
            break
        }
        sleep(1)
    }

    // Post the info to the log
    log.info("Set new token: " + authProfile.getAccessToken())
}
Preerab
Occasional Contributor

SubmitListener.beforeSubmit  -> with the script works like charm.

I need to tweek authProfile to get custmoized name. It works now.

 

thanks all, appreciate all your input

Nastya_Khovrina
SmartBear Alumni (Retired)

You're welcome!


Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
cancel
Showing results for 
Search instead for 
Did you mean: