I am using ReadyAPI (SoapUI - pro) and able to manually retrieve the OAuth2.0 Access Token for grant type Client Credentials.
How can I make this Access Token retrieval automated in Groovy script so that I could run it from command line without GUI interaction. The Groovy code I am using is:
@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 = '*************-ac6efba39e6a'
def clientSecret = '**********_icH6Nr6.7'
def tokenUrl = 'https://login.microsoftonline.com/*******/oauth2/v2.0/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://***-npd.az****.ca')
http.request(Method.GET, ContentType.JSON) { req ->
headers['Authorization'] = "Bearer $accessToken"
response.success = { resp, reader ->
// Handle the API response
println resp.data
}
}
}
When I run this code, I get this error:
java.lang.NoClassDefFoundError: org/apache/ivy/util/MessageLogger

It looks like you are missing .jar in your Groovy setup.
Check the answers in this thread. They might help.
Groovy @Grab NoClassDefFoundError: org/apache/ivy/plugins/resolver/DependencyResolver