Forum Discussion

demosoapuser's avatar
demosoapuser
Occasional Contributor
8 years ago

Dynamically Set client credentials

Currently, we are using Oauth2 Client Credentials Grant on our Apis. Is there a way to set or automate the values on the Authorization Profile credentials fields dynamically (ex. values from Datasource or Properties)? Below is the screenshot of Authorization Profile:

  • Hi New2API,

     

    To change the credentials you have set in the authorization profile (for the Client Credentials Grant type) you can use the following script in the Groovy Test Step:

    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("YourAuthProfile");
    oAuthProfile.setClientSecret("Secret");
    oAuthProfile.setClientID("Identification");
    
    // the following code for getting new access token
    def oAuthClientFacade = new OltuOAuth2ClientFacade(TokenType.ACCESS);
    oAuthClientFacade.requestAccessToken(oAuthProfile, true);

     

    • New2API's avatar
      New2API
      Frequent Contributor

      Thanks nmrao for the suggestion. I was able to retrieve access token using global properties. 

      I was wondering if you could help me or show some pointers (apis) to automate this using groovy? or Java script.

       

      thanks!

      • Nastya_Khovrina's avatar
        Nastya_Khovrina
        SmartBear Alumni (Retired)

        Hi New2API,

         

        To change the credentials you have set in the authorization profile (for the Client Credentials Grant type) you can use the following script in the Groovy Test Step:

        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("YourAuthProfile");
        oAuthProfile.setClientSecret("Secret");
        oAuthProfile.setClientID("Identification");
        
        // the following code for getting new access token
        def oAuthClientFacade = new OltuOAuth2ClientFacade(TokenType.ACCESS);
        oAuthClientFacade.requestAccessToken(oAuthProfile, true);

         

  • spoot99's avatar
    spoot99
    Senior Member

    Hi There, 

    I was looking for the exact same answer as the question listed above. Thanks a lot for the reply, it gave me direction to look further.

     

    I was able to create a GroovyScript and parametrize the Auth Entry. 

     

    """"

    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("Name");


    oAuthProfile.setClientID("client id")
    oAuthProfile.setClientSecret("client secret")
    oAuthProfile.setScope("scope")
    oAuthProfile.setAuthorizationURI("authorization URL" )
    oAuthProfile.setAccessTokenURI("AccessToken URL" )
    oAuthProfile.setRedirectURI("Redirect URL" )

     

    oAuthProfile.setAutomationJavaScripts(["AutomationPage1", "AutomationPage2"])

     

    //Run the Entry

    def oAuthClientFacade = new OltuOAuth2ClientFacade(TokenType.ACCESS);
    oAuthClientFacade.requestAccessToken(oAuthProfile, true);

    """"

     

    Best of luck!