Access tokens using 2 legged Oauth 2.0 and Apache OauthClient

Monday, May 5, 2014

I'm trying get 2 legged Oauth 2 working. I'm trying to mimic this CURL call to get an access token:



curl -u CLIENT_ID:CLIENT_SECRET http://ift.tt/1kFFSum -d "grant_type=client_credentials"


I'm trying to do the same thing in Java using Apache Oltu:



<dependency>
<groupId>org.apache.oltu.oauth2</groupId>
<artifactId>org.apache.oltu.oauth2.client</artifactId>
<version>1.0.0</version>
</dependency>


This is the Java code I'm using:



OAuthClientRequest request = OAuthClientRequest
.tokenLocation("http://ift.tt/1kFFSum")
.setGrantType(GrantType.CLIENT_CREDENTIALS)
.setClientId(CLIENT_ID)
.setClientSecret(CLIENT_SECRET)
.buildBodyMessage();


//create OAuth client that uses custom http client under the hood
OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient());

OAuthJSONAccessTokenResponse oAuthResponse = oAuthClient.accessToken(request, OAuthJSONAccessTokenResponse.class);


The CURL command works fine but the Java code gives this error:



OAuthProblemException{error='invalid_request', description='Must include a basic access authentication header.', uri='null', state='null', scope='null', redirectUri='null', responseStatus=0, parameters={}}


I tried using the header message build:



.buildHeaderMessage();


instead but it gives:



OAuthProblemException{error='invalid_request', description='Must specify grant_type field.', uri='null', state='null', scope='null', redirectUri='null', responseStatus=0, parameters={}}


Any suggestions are appreciated. I would expect this to be pretty straightforward.







http://ift.tt/1iScqpm