cancel
Showing results for 
Search instead for 
Did you mean: 

How to really get the OAuth 2 token automatically

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

import com.eviware.soapui.impl.rest.actions.oauth.OltuOAuth2ClientFacade
import com.eviware.soapui.support.editor.inspectors.auth.TokenType

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.setClientSecret(clientSecret);
oAuthProfile.setClientID(clientID);
log.info("Client Secret:"+clientSecret)
log.info("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)
log.info("Access Token:"+accessToken)

 

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Occasional Contributor

Re: How to really get the OAuth 2 token automatically

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.

View solution in original post

12 REPLIES 12
Highlighted
Occasional Contributor

Re: How to really get the OAuth 2 token automatically

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
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())
}

 

Cheers,

Cekay

Highlighted
Occasional Contributor

Re: How to really get the OAuth 2 token automatically

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.

Highlighted
Occasional Contributor

Re: How to really get the OAuth 2 token automatically

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 🙂

 

Highlighted
Occasional Contributor

Re: How to really get the OAuth 2 token automatically

Cekay,

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

Highlighted
Occasional Contributor

Re: How to really get the OAuth 2 token automatically

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

 

 

Highlighted
Occasional Contributor

Re: How to really get the OAuth 2 token automatically

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

Occasional Contributor

Re: How to really get the OAuth 2 token automatically

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

 

 

 

 

Highlighted
Moderator

Re: How to really get the OAuth 2 token automatically

@BEdwards 

Here is the example and link to the event handlers article: https://support.smartbear.com/readyapi/docs/projects/requests/auth/types/oauth2/automate/sample.html...



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

Re: How to really get the OAuth 2 token automatically

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.

View solution in original post

New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors