Forum Discussion

davecoleman's avatar
davecoleman
Contributor
5 years ago
Solved

Managing multiple Auth records - Ability to trigger one but how to complete for subsequent

  I have setup the script below to request the token for one of our Endpoints within our project This works well for that endpoint but we have multiple API's within the project and so multiple Auth...
  • KirillZakharov's avatar
    KirillZakharov
    5 years ago

    Hi davecoleman 

     

    Try to use this script:

    // 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 authType = submit.getRequest().getAuthType()
    if(authType == "OAuth 2.0"){ // Set up variables
    def authProfileName = submit.getRequest().getActiveAuthProfile().getName() 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()) }
  • KirillZakharov's avatar
    KirillZakharov
    5 years ago

    Hi davecoleman,

     

    The script which I provided generates a token depending on which authentication profile is set. So if your profile is "profile1", then the token will be generated for this profile:

    def authType = submit.getRequest().getAuthType()
    if(authType == "OAuth 2.0"){ // Set up variables
    def authProfileName = submit.getRequest().getActiveAuthProfile().getName()

     

    Here, the script gets the type of auth in the first row and only if it's OAutn 2.0 then it will generate the token for the profile that is indicated in the auth setting for the request.

     

    If it didn't work for you, let me know what did you mean "2 different OAuth tokens to request to access these API's", do you mean two tokens in one request at the same time?