title: 401 unauthorized error for GET request with type Authorization = bearer token
body: <p>I am making an HttpGet request with "Authorization" as header attaching bearer token. I am getting a 401 unauthorized error all the time.</p>
  
  <p>I have tried retrofit, it didn't work so I reverted back to basic HTTP client. Then after a lot of research found that there is some error with DefaultHttp. So I changed to HTTPUrlConnection. Even after doing all these, I am still getting an unauthorized 401 error. What could I possibly do wrong here? Because this error still persists, I decided to stick to HTTPUrlConnection or DefaultHTTP and stay away from libraries.</p>
  
  <p>Here is my code with HttpUrlConnection</p>
  
  <pre><code>        URL obj = new URL(url);
          HttpURLConnection con = (HttpURLConnection)
                              obj.openConnection();
          con.setRequestMethod("GET");
  
          String authString = "Bearer" + accessToken;
          con.setRequestProperty("Authorization", authString);
  
          int responseCode = con.getResponseCode();
  
          if (responseCode == HttpURLConnection.HTTP_OK) { // success
              BufferedReader in = new BufferedReader(new InputStreamReader(
                      con.getInputStream()));
              String inputLine;
              response = new StringBuffer();
  
              while ((inputLine = in.readLine()) != null) {
                  response.append(inputLine);
              }
              in.close();
  
          } else {
              System.out.println("GET request not worked");
          }
  </code></pre>
  
  <p>Here is my DefaultHttp request</p>
  
  <pre><code>        StringBuilder builder = new StringBuilder();
          HttpClient client = new DefaultHttpClient();
          HttpGet httpGetRequest = new HttpGet(url);
          httpGetRequest.addHeader("Authorization","Bearer"+accessToken);
          try {
              HttpResponse response = client.execute(httpGetRequest);
              StatusLine statusLine = response.getStatusLine();
              int statusCode = statusLine.getStatusCode();
              if (statusCode == 200) {
                  HttpEntity entity = response.getEntity();
                  InputStream content = entity.getContent();
                  BufferedReader reader = new BufferedReader(new 
                  InputStreamReader(content));
                  String line;
                  while ((line = reader.readLine()) != null) {
                      builder.append(line);
                  }
              }
  </code></pre>