I am trying to access the API using the oauth2 authorization token in Java. Here is the client code
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost post = new HttpPost(http:
post.setHeader("Content-Type","application/json");
post.setHeader("Authorization", "Bearer " + finalToken);
JSONObject json = new JSONObject();
StringEntity params = new StringEntity(json.toString());
post.setEntity(params);
HttpResponse response = httpclient.execute(post);
httpclient.getConnectionManager().shutdown();
This returns 401.
The equivalent curl command works without problems with the same token:
curl -H "Content-Type:application/json" -H "Authorization:Bearer randomToken" -X POST -d @example.json http://rest-api
I tried to execute the request and it looks like authorization is set correctly
DEBUG [2016-06-28 20:51:13,655] org.apache.http.headers: >> Authorization: Bearer authRandomToKen; Path=/; Domain=oauth2-server; Expires=Wed, 29 Jun 2016 20:51:13 UTC
I tried the curl command by copying the same token and t works fine
Although I also see this line
DEBUG [2016-06-28 20:51:13,658] org.apache.http.impl.client.DefaultHttpClient: Response contains no authentication challenges
source
share