Forum Discussion

Satheesh-raju's avatar
Satheesh-raju
Occasional Contributor
2 years ago

Automating OAuth 2.0 with Client credentials grant

Hi All,

 

Am having an OAuth 2.0 authorization with Client Credentials Grant used in my API. I configured the authorization profile in the Auth manager as shown below and the token is expected to refresh automatically as per the settings (Shown Below).

But noticed that the token is not automatically refreshed when the test suite is ran and every time before executing the test suite I need to manually create new token using the get token option. 

Is there any option in automating the process is creating the authorization token ?

I tried automating the same by adding a groovy script to the test suite. but getting the error " java.lang.NoClassDefFoundError: org/apache/ivy/util/MessageLogger"

PFB the groovy script used:

@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7.1')

import groovyx.net.http.HTTPBuilder
import groovyx.net.http.Method
import groovyx.net.http.ContentType

def clientId = 'Your_ClientID'
def clientSecret = 'Your_Client_Screctid'
def tokenUrl = 'https://dev.test.com/oauth2/token'

// First, we obtain an access token using the client credentials grant
def http = new HTTPBuilder(tokenUrl)
http.request(Method.POST, ContentType.URLENC) { req ->
    body = [
        grant_type: 'client_credentials',
        client_id: clientId,
        client_secret: clientSecret
    ]
    response.success = { resp, reader ->
        // Extract the access token from the response
        def accessToken = resp.data.access_token
        // Now we can use the access token to make API requests
        makeApiRequest(accessToken)
    }
}

// Finally, we can use the access token to make API requests
def makeApiRequest(accessToken) {
    def http = new HTTPBuilder('https://api.example.com')
    http.request(Method.GET, ContentType.JSON) { req ->
        headers['Authorization'] = "Bearer $accessToken"
        response.success = { resp, reader ->
            // Handle the API response
            println resp.data
        }
    }
}

 

7 Replies

  • nmrao's avatar
    nmrao
    Champion Level 3

    By the default, there might be a library whose name contains ivy under READYAPI_HOME/lib directory.

    Do you see it?

    • Satheesh-raju's avatar
      Satheesh-raju
      Occasional Contributor

      Hi nmrao ,

      Checked the lib folder, no files present with name containing 'ivy'.

       

  • nmrao's avatar
    nmrao
    Champion Level 3
    Then download compatible apache-ivy jar file and copy it under READYAPI_HOME/bin/ext directory and restart the tool.
  • sp00ky's avatar
    sp00ky
    Occasional Contributor

    Same problem for me...

     

    1st simple question: Why OAuth 2.0 access token refresh is not working automatically in ReadyAPI 3.47.0? Only manual is working.

     

    In addition I also tried to add latest Apache ivy dependency manager "ivy-2.5.1.jar" to "C:\Program Files\SmartBear\ReadyAPI-3.47.0\bin\ext", but grab command will not work:

     

    Wed Jul 05 11:37:12 CEST 2023: ERROR: An error occurred in the script of the Groovy Script test step [Groovy Script]:
    Wed Jul 05 11:37:12 CEST 2023: ERROR: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
    Script13.groovy: 1: Unexpected input: '@Grab(group='com.github.groovy-wslite', module='groovy-wslite', version='1.1.3')' @ line 1, column 81.
    oovy-wslite', version='1.1.3')

     

    Thank you in advance!

    sp00ky

  • sp00ky's avatar
    sp00ky
    Occasional Contributor

    ok, why? it the same problem and it is not solved or?

     

    br

    sp00ky

    • sp00ky's avatar
      sp00ky
      Occasional Contributor

      Done!

      But I thought its the better forum etiquette to not open too much topics if already a similar exists.

       

      Br
      sp00ky