Skip to content

Instantly share code, notes, and snippets.

@danielnegri
Created June 23, 2015 17:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielnegri/d618aed8e3eaf30be92e to your computer and use it in GitHub Desktop.
Save danielnegri/d618aed8e3eaf30be92e to your computer and use it in GitHub Desktop.
Example token request with a client credentials grant
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