Created
June 23, 2015 17:44
-
-
Save danielnegri/d618aed8e3eaf30be92e to your computer and use it in GitHub Desktop.
Example token request with a client credentials grant
This file contains 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 com.nimbusds.oauth2.sdk | |
import com.nimbusds.oauth2.sdk.auth | |
import com.nimbusds.oauth2.sdk.http | |
import com.nimbusds.oauth2.sdk.id | |
import com.nimbusds.oauth2.sdk.token | |
// Construct the client credentials grant | |
AuthorizationGrant clientGrant = new ClientCredentialsGrant(); | |
// The credentials to authenticate the client at the token endpoint | |
ClientID clientID = new ClientID("super-client"); | |
Secret clientSecret = new Secret("super-secret"); | |
ClientAuthentication clientAuth = new ClientSecretBasic(clientID, clientSecret); | |
// The request scope for the token (may be optional) | |
Scope scope = new Scope("read", "write"); | |
// The token endpoint | |
URI tokenEndpoint = new URI("https://auth.ondeck.tools/token"); | |
// Make the token request | |
TokenRequest request = new TokenRequest(tokenEndpoint, clientAuth, clientGrant, scope); | |
TokenResponse response = TokenResponse.parse(request.toHTTPRequest().send()); | |
if (! response.indicatesSuccess()) { | |
// We got an error response... | |
TokenErrorResponse errorResponse = (TokenErrorResponse) response; | |
} | |
AccessTokenResponse successResponse = (AccessTokenResponse) response; | |
// Get the access token | |
AccessToken accessToken = successResponse.getAccessToken(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment