-
-
Save dzmitry-savitski/0c9c7f54ad8c1c87fad44fa9b28924a7 to your computer and use it in GitHub Desktop.
ig
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import org.forgerock.http.protocol.* | |
| import org.forgerock.openig.http.* | |
| // Replace with your actual values | |
| def clientId = "my-client-id" | |
| def clientSecret = "my-client-secret" | |
| def tokenEndpoint = "https://am.example.com/oauth2/access_token" | |
| // Prepare request | |
| def tokenRequest = new Request() | |
| tokenRequest.uri = tokenEndpoint | |
| tokenRequest.method = "POST" | |
| tokenRequest.headers.put("Content-Type", "application/x-www-form-urlencoded") | |
| tokenRequest.entity = "grant_type=client_credentials&client_id=${clientId}&client_secret=${clientSecret}" | |
| // Send the request using IG's internal HTTP client | |
| def response = client.call(tokenRequest) | |
| // Parse the access token | |
| if (response.status.successful) { | |
| def json = new JsonSlurper().parseText(response.entity.string) | |
| def accessToken = json.access_token | |
| logger.info("Got token: " + accessToken) | |
| return new Response(Status.OK).setEntity("Access token: ${accessToken}") | |
| } else { | |
| logger.error("Token request failed: ${response.status}") | |
| return new Response(Status.UNAUTHORIZED).setEntity("Failed to get token") | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment