Forum Discussion
Hi Humashankar ,
Thank you for your reply.
I looked into Groovy script. Did you mean something like this?
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7.1')
import groovyx.net.http.RESTClient
def clientId = "a1b2"
def redirectUri = "https://readyapi/callback"
def authorizationUrl = "https://abc.com/authorize"
def authUrl = "${authorizationUrl}?client_id=${clientId}&redirect_uri=${redirectUri}&response_type=token"
def accessToken = ""
def url = new URL(redirectUri)
def params = url.fragment.split('&')
params.each { param ->
def parts = param.split('=')
if (parts[0] == 'access_token') {
accessToken = parts[1]
}
}
if (accessToken) {
def restClient = new RESTClient('')
restClient.auth.oauth2(accessToken)
def response = restClient.get(path: '/resource', contentType: 'application/json')
println "Response: ${response.status} - ${response.data}"
} else {
println "Failed to retrieve access token"
}
Best regards,
aa1
Hi aa1
Your script targetting for OAuth 2.0 access token from an authorization server, then using that token to make authenticated requests to a resource server. It should work / if not try make some adjustments by
Replace 'your_access_token_here' with the actual access token you have obtained from your OAuth flow
- aa18 months agoContributor
Hi Humashankar
I tried the following but it gives me an error message. Something is missing in my code.
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7.1');
import groovyx.net.http.RESTClient;
import groovyx.net.http.HTTPBuilder
import groovyx.net.http.Method
import groovyx.net.http.ContentTypedef clientId = "5LM";
def redirectUri = "https://readyapi/callback";
def authorizationUrl = "https://abc.com/authorize";
def authUrl = "${authorizationUrl}?client_id=${clientId}&redirect_uri=${redirectUri}&response_type=token";
//def clientSecret = ?;
def http = new HTTPBuilder(tokenUrl)
http.request(Method.POST, ContentType.URLENC) { req ->
body = [
grant_type: 'client_credentials',
client_id: clientId
]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 requestsprintln accessToken
//println "Please visit the following URL to grant access:\n${authUrl}";
This is the error I get:
The following script is invalid: @Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7.1'); import groovyx.net.http.RESTClient; import groovyx.net.http.HTTPBuilder import groovyx.net.http.Method import groovyx.net.http.ContentType def clientId = "5LM"; def redirectUri = "https://readyapi/callback"; def authorizationUrl = "https://abc.com/authorize"; def authUrl = "${authorizationUrl}?client_id=${clientId}&redirect_uri=${redirectUri}&response_type=token"; //def clientSecret = ?; def http = new HTTPBuilder(tokenUrl) http.request(Method.POST, ContentType.URLENC) { req -> body = [ grant_type: 'client_credentials', client_id: clientId ] 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 println accessToken //println "Please visit the following URL to grant access:\n${authUrl}";
Error: missing ; before statement (scriptToValidate#2)
- Humashankar8 months agoChampion Level 3
Hi aa1
The error message indicates that there is a missing semicolon (;) before a statement in the script.
Looks the body variable is defined without a semicolon at the end of the line.
Try adding a semicolon after body = [ grant_type: 'client_credentials', client_id: clientId ] like this:
body = [ grant_type: 'client_credentials', client_id: clientId ];
This should fix the error is what I believe – Give a try and let me know
- aa18 months agoContributor
Thank you for your reply. I still get the same error message even after adding the semi colon as you suggested. There should be something I am not doing correctly.
Related Content
- 4 years ago
Recent Discussions
- 5 days ago
- 9 days ago