Created
October 25, 2019 14:06
-
-
Save malys/054adf42a70cfce401a0fe7563e1b2e3 to your computer and use it in GitHub Desktop.
[Client Credentials] #keycloak #client
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
| @Grab('com.github.scribejava:scribejava-apis:6.9.0') | |
| import com.github.scribejava.apis.KeycloakApi; | |
| import com.github.scribejava.core.builder.ServiceBuilder; | |
| import com.github.scribejava.core.model.OAuth2AccessToken; | |
| import com.github.scribejava.core.model.OAuthRequest; | |
| import com.github.scribejava.core.model.Response; | |
| import com.github.scribejava.core.model.Verb; | |
| import com.github.scribejava.core.oauth.OAuth20Service; | |
| import java.io.IOException; | |
| import java.util.Scanner; | |
| import java.util.concurrent.ExecutionException; | |
| // Replace these with your own api key, secret, callback, base url and realm | |
| final String apiKey = "clientId"; | |
| final String apiSecret = "client secret"; | |
| final String baseUrl = "https://rh-sso server"; | |
| final String realm = "realm"; | |
| final String protectedResourceUrl = baseUrl + "/auth/realms/" + realm + "/protocol/openid-connect/userinfo"; | |
| final OAuth20Service service = new ServiceBuilder(apiKey) | |
| .apiSecret(apiSecret) | |
| .defaultScope("openid") | |
| .build(KeycloakApi.instance(baseUrl, realm)); | |
| System.out.println("=== Keyloack's OAuth Workflow ==="); | |
| System.out.println(); | |
| OAuth2AccessToken accessToken = service.getAccessTokenClientCredentialsGrant(); | |
| System.out.println(accessToken.getExpiresIn()) | |
| accessToken = service.refreshAccessToken(accessToken.getRefreshToken()); | |
| System.out.println("(The raw response looks like this: " + accessToken.getRawResponse() + "')"); | |
| System.out.println("Now we're going to access a protected resource..."); | |
| final OAuthRequest request = new OAuthRequest(Verb.GET, protectedResourceUrl); | |
| service.signRequest(accessToken, request); | |
| Response response = service.execute(request) | |
| System.out.println("Got it! Lets see what we found..."); | |
| System.out.println(); | |
| System.out.println(response.getCode()); | |
| System.out.println(response.getBody()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment