cancel
Showing results for 
Search instead for 
Did you mean: 

how to automate our manual Oauth2.0 process. ?

SOLVED
Highlighted
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

 

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Moderator

Re: how to automate our manual Oauth2.0 process. ?

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?



Learn SmartBear products in a fun and easy way and prove your knowledge!
>>Participate in the TechCorner Challenge today

View solution in original post

8 REPLIES 8
Highlighted
Contributor

Re: how to automate our manual Oauth2.0 process. ?

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

Highlighted
Occasional Contributor

Re: how to automate our manual Oauth2.0 process. ?

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,

Highlighted
Community Manager

Re: how to automate our manual Oauth2.0 process. ?

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, @msiadak, more ideas?

---------
Tanya Gorbunova
SmartBear Community Manager

Learn SmartBear products in a fun and easy way and prove your knowledge!
>>Participate in the TechCorner Challenge today
Highlighted
Occasional Contributor

Re: how to automate our manual Oauth2.0 process. ?

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.

Highlighted
Moderator

Re: how to automate our manual Oauth2.0 process. ?

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?



Learn SmartBear products in a fun and easy way and prove your knowledge!
>>Participate in the TechCorner Challenge today

View solution in original post

Highlighted
Occasional Contributor

Re: how to automate our manual Oauth2.0 process. ?

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())
}
Highlighted
Occasional Contributor

Re: how to automate our manual Oauth2.0 process. ?

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

Highlighted
Moderator

Re: how to automate our manual Oauth2.0 process. ?

You're welcome!



Learn SmartBear products in a fun and easy way and prove your knowledge!
>>Participate in the TechCorner Challenge today
New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors