Search code examples
javacurluber-api

HttpGet unable to setHeader properly


I am working on Uber API's and trying to get user profile using instructions from here

following curl command works fine and can get user information from uber:

curl -H "Authorization: Bearer YOUR_ACCESS_TOKEN" "https://api.uber.com/v1/me"

However, my Java code is not working - but receiving 401 response from server.

HttpGet httpGetRequest = new HttpGet("https://api.uber.com/v1/me");
httpGetRequest.setHeader("Authorization: Bearer", accessTokenModel.getAccess_token());
HttpResponse httpResponse = client.execute(httpGetRequest);
response.getWriter().println("httpget response:" + httpResponse.getStatusLine().getStatusCode());

Any idea why I am getting error on my GET request. I also tried HttpGet addHeader method to add the header but same result.


Solution

  • the header is "Authorization" and the value is "Bearer XXXX". So

    httpGetRequest.setHeader("Authorization: Bearer", accessTokenModel.getAccess_token());
    

    should be

    httpGetRequest.setHeader("Authorization", "Bearer " + accessTokenModel.getAccess_token());