Forum Discussion

Rmuenks1's avatar
Occasional Contributor
5 years ago

OAuth 2 Automation and JDBC calls

Hi, I'm creating an OAuth 2 automation script to handle our login screen when calling an /api/jwt token retrieval.  When getting the token, our login pages appear, for which I have added 4 pages of Automation to handle entering User Name, Password, etc.


Page 1: Enter User Name screen

Page 2: Enter Password screen

Page 3: Request security token via Email or Text

Page 4: Enter valid security code which was received via Email or Text, and press Next


Once I reach Page 4, I realized I had no way of getting the security code which our website sends me. 


So I began to consider my options.  I felt like a JDBC call would work great and I could get the security token directly from the database.  When I tried creating JDBC calls within the OAuth Automation script profile, it began to throw all sorts of errors, not being able to import groovy.sql.Sql, seeing a Sql.newInstance call as ReferenceError: Can't find variable: Sql]


I thought maybe if I could call an outside Groovy script to return the value I might try that.  So I created a script file which works, but attempting this type of code in the OAuth 2 automation screen fails.  


MyDBServer = "<removed>"
MyUser = "<removed>"

MyDBName = "<removed>"


Mysql = Sql.newInstance('jdbc:sqlserver://' + MyDBServer + ':1433;databaseName='+MyDBName+';integratedSecurity=true','','','')
Mydata = Mysql.firstRow("SELECT MyCode FROM secured.MyAccessCodes AC JOIN secured.MyTable US ON AC.MyID = US.MyID Where US.UserName = '"+MyUser+"' and AC.ActiveFlag = 1")


Any suggestions?  I'd like to be able to add the database call directly into Step 4 of the OAuth 2 automation script in order to enter the security code into the window.  

8 Replies

  • richie's avatar
    Community Hero
    Hi Rmuenks1,

    I'm not a coder so i can't help with your groovy, but i wanted to confirm some points to rule out any issues with the SQL.

    Your SQL is:
    SELECT mycodes
    FROM secured.myaccesscodes ac
    secured.mytable us
    ac.myid = us.myid
    WHERE us.username = "'+myuser+'"
    AND ac.activeflag = 1

    Some RDBMSs struggle if you use partial aliasing. You've aliased everything but the initial column you are retrieving in your result set, so rather than 'select mycodes from', instead use 'select ac.mycodes from'.

    Secondly i just wanted to ensure your notation is correct relative to the FROM and JOIN clauses.
    Your FROM/JOIN clauses read

    'FROM secured.myaccesscodes ac JOIN secured.mytable us'

    Can you confirm 'secured' is the database name, so 'myaccesscodes' and 'mytable' are the table names? I struggled before using an earlier version of SoapUI (not ReadyAPI!) when i included the database name in the table identifier in my SQL. Once i removed the database name reference so my table identifiers included only the table, my SQL worked fine, but didn't when i included the databasename.

    Cheers, sorry i can't comment on anything else,

    • Rmuenks1's avatar
      Occasional Contributor

      Thanks for the feedback regarding SQL, but this code works in a Groovy script. 


      What I need help with is understanding in what ways the script editor inside the OAuth 2 profile GUI differs from the capabilities of a script inside a Groovy script. Also, if there is a limitation on what you can do within the OAuth 2 automation profile scripting, what options exist for making a SQL database call from within the automation profile to get our unique 6-digit security token during OAuth 2 automation profile to validate the user?


      When running the code in the original post in the OAuth2 automation scripts GUI I get an error that says:

      ReferenceError: Can't find variable: Sql]


      if I use an "import Groovy.sql.Sql" at the beginning I get an error that says:  

      Error: syntax error (scriptToValidate#1)


      So, it seems like the "Automation scripts for OAuther 2 profile" screen has some limitations on what sort of code can execute within it, but I'm not finding anything in the documentation, or online, to indicate that limitation.


      Thanks again!

      • nmrao's avatar
        Champion Level 3



        Would you please provide the following?


        1. Screen shot showing groovy script test along with script (fully visible). Also appreciate if you can attach the full script in text mode which will quickly help try.


        2. Assuming that you are using MS SQL Server. What driver / library files available under ReadyAPI?


        3. Full stacktrace of the error.