Forum Discussion

BEdwards's avatar
BEdwards
Occasional Contributor
6 years ago

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)

 

  • BEdwards's avatar
    BEdwards
    6 years ago

    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.

  • Cekay's avatar
    Cekay
    Contributor

    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

    • BEdwards's avatar
      BEdwards
      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.

      • Cekay's avatar
        Cekay
        Contributor

        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 :)