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)


SmartBear Alumni (Retired)



The script provided by you works fine for me in the Groovy Script test step

Also, the script provided by @Cekay works fine for me in the SubmitListener.beforeSubmit event

But, the Authentication profile should be set up correctly. 


Can you get the access token in the UI? It is expected that you see the browser. Can you successfully log in there?


After you retrieve the token once you will be able to automatically refresh it using the "Automation" script (using JavaScript on the screen with Pages). For example:


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

Hey Nastya,

We don't have the resourse owners ID or password, so we have to use "Authorization Code Grant", there is no logon screen, it just pops up the code.  I want to grab that from the browser, place it in a variable and close the browser.  Not sure how to do that.

SmartBear Alumni (Retired)

@BEdwards So, your OAuth token is returned in JSON, not in the redirect URL. It seems that it's not fully supported by ReadyAPI now. 

I recommend that you open a new support case here:

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