Ask a Question

How to really get the OAuth 2 token automatically

Occasional Contributor

How to really get the OAuth 2 token automatically

I wrote some code that gets an access token. When the code runs, a browser displays on the screen which contains the access token.

But when I try to get the access token and log it, a null string is being displayed. Also, I do not know if there is a way to force the browser to close using my code. Right now when I run this code, the browser window opens but I have to click on it to close it.

Could you please let me know what I am doing wrong ?


Groovy Code:


def project = context.getTestCase().getTestSuite().getProject();
def oAuthProfile = project.getAuthRepository().getEntry("IMAGEN_Profile");
def clientSecret = testRunner.testCase.testSuite.getPropertyValue("Client_Secret")
def clientID = testRunner.testCase.testSuite.getPropertyValue("Client_ID")
oAuthProfile.setClientID(clientID);"Client Secret:"+clientSecret)"Client ID:"+clientID)

// the following code for getting new access token
def oAuthClientFacade = new OltuOAuth2ClientFacade(TokenType.ACCESS);
oAuthClientFacade.requestAccessToken(oAuthProfile, true);
def accessToken = oAuthProfile.getAccessToken()
testRunner.testCase.testSuite.setPropertyValue("Auth_Code",accessToken)"Access Token:"+accessToken)



I can't tell you what is wrong but I can post our script. :X Maybe it helps.

I didn't write it, so I can't answer any questions


We added this script as Event "SubmitListener.beforeSubmit


// Import the required classes

def authProfileName = "PROFILE_NAME"

    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

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

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




Occasional Contributor

Thank Cekay, that didn't work for me, I must be missing something.  First of all, all of the if code that is associated with the submit. didn't work.  When I commented that out, the token is still blank.  Do you have any Java code on the OAuth 2 script for page 1 and page 2.

I'm sorry that the script didn't work for you. I just checked the Auth Manager but there is no additional script.

I hope some else is able to help you 🙂


Occasional Contributor


I've never worked with Event Handlers, so it is probably my problem.  Can anybody guide me?

I'll try my best. There is not much to guide

  1. Click on "Events" in your SoapUI - It will open a new window
  2. Click on the green plus, to add a new event
  3. Choose "SubmitListener.beforeSubmit"
  4. Click OK and copy paste the script
  5. Change in line 6: PROFILE_NAME to the name of your auth-profile
  6. Save script via clicking OK


That is basically everything. Please find attached screenshot for more details ,

Run your testcase, where you need to get a token, and let the magic work for you 😉


I copied the script from my colleque, who is working on another project. All I did is to change the profile name so I think you don't have to change anything else. If this not working, you should show your error log.


Good Luck and have a nice weekend



Occasional Contributor

I have a question, where does the token go?  Where do I retrieve it to put into my "Authorization" part of the header.  I see in the loop that it waits for a new token and then just ends...

Ah so that is the problem. Maybe we use the token in different ways. I don't have put it anywhere.

I have a testCase with a put rest request and use for the authorization my auth profile, which I used in the script.

It will generate the token for this profile. We don't have to send it via header.


When I start the testCase I will get a new token and it will be used directly.


See screenshot again please


So now we reached the end of my knowledge. If this is still not working for you, someone else has to help =/ Sorry





SmartBear Alumni (Retired)


Here is the example and link to the event handlers article:

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

Thanks @Nastya_Khovrina,

I've read this article about 40 times and get nothing from it.  I'm lost in the Auth Manager, I can't get an access token unless I use "Authorization Code Grant" and it only gives it to me in a web browser that I can do nothing with. Are there any classes on OAuth 2 with ReadyAPI?  Maybe that would be helpful.  Like a course for dummies.  I'm not a dummy, but don't deal with websites very often, mostly do background stuff in PowerShell, Ruby, VBA and such.

Showing results for 
Search instead for 
Did you mean: