Forum Discussion
Hi aa1
Uncaught Typeerror:loginBtn.click in not a function
suggests that the click() function is not available on the loginBtn element.
Which means that the loginBtn variable is not referencing the correct element or it's not being selected properly.
So basically ReadyAPI operates outside of a web browser context and typically uses Groovy scripting rather than JavaScript, directly interacting with DOM elements like you would in a web browser won't work
Recommending to execute automate OAuth 2.0 authentication in ReadyAPI using Groovy scripting
Hope this helps - Happy to help further!!
Thank you very much and have a great one!
Warm regards
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
- Humashankar9 months agoChampion Level 3
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
Related Content
- 4 years ago
Recent Discussions
- 5 days ago
- 9 days ago