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 https://mydomain.com/token -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("https://mydomain.com/token")
.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.