Java client example using OAuth2
From DarkWiki
This example shows how to make a RESTful call using OAuth2 from Spring.
NB. I came across a bug where the server said it supported a token_type of "bearer", but actually only supported "Bearer". To get around it, I had to implement my own OAuth2RequestAuthenticator to force the case.
The code
Maven pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>darkmine</groupId>
<artifactId>darkmine-oauth2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
...
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
<version>2.0.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.7.3</version>
</dependency>
...
</dependencies>
</project>
Java client
Main program.
package org.darkmine.demo.oauth;
import org.springframework.http.ResponseEntity;
import org.springframework.security.oauth2.client.DefaultOAuth2ClientContext;
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
import org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails;
import org.springframework.web.util.UriComponentsBuilder;
import org.darkmine.demo.oauth.dto.Person;
import org.darkmine.demo.oauth.BearerAuthenticator;
public class OAuth2Demo {
public static void main(String[] args) {
// Setup
String clientId = "CLIENT_ID";
String clientSecret = "<SUPER_SECRET_CODE>";
String url = "https://www.myprogram.url";
// Prepare the credentials resource.
ClientCredentialsResourceDetails resourceDetails = new ClientCredentialsResourceDetails();
resourceDetails.setClientSecret(clientSecret);
resourceDetails.setClientId(clientId);
resourceDetails.setGrantType( "client_credentials" );
resourceDetails.setAccessTokenUri( url + "/oauth");
// Prepare our template and context (although the context isn't really needed in this example).
DefaultOAuth2ClientContext clientContext = new DefaultOAuth2ClientContext();
OAuth2RestTemplate oAuthRestTemplate = new OAuth2RestTemplate(resourceDetails,clientContext);
oAuthRestTemplate.setAuthenticator( new BearerAuthenticator() );
// Go fetch an access token.
System.out.println( "AccessToken: " + oAuthRestTemplate.getAccessToken() );
// Prepare our RESTful URL
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl( url + "/people");
builder.queryParam( "countrycode" , "GBR" );
// Make our API call.
ResponseEntity<Person[]> pudoLocationsResponse = oAuthRestTemplate.getForEntity( builder.build().toUri() , Person[].class );
}
}
This second class is used to get around a bug with some servers where the server said it supported a token_type of "bearer", but actually only supported "Bearer". Therefore we implement our own OAuth2RequestAuthenticator.
Lastly, setting the content-type here is not strictly necessary.
package org.darkmine.demo.oauth;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpRequest;
import org.springframework.security.oauth2.client.OAuth2ClientContext;
import org.springframework.security.oauth2.client.OAuth2RequestAuthenticator;
import org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails;
public class BearerAuthenticator implements OAuth2RequestAuthenticator {
@Override
public void authenticate(OAuth2ProtectedResourceDetails resource, OAuth2ClientContext clientContext,ClientHttpRequest request) {
request.getHeaders().set( "Authorization" , "Bearer " + clientContext.getAccessToken() );
request.getHeaders().setContentType(MediaType.APPLICATION_JSON);
}
}