Code:
private void getResponse(LoginBean object) { String auth = object.getUsername() + ":" + object.getPassword(); byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(Charset .forName("US-ASCII"))); String authHeader = "Basic " + new String(encodedAuth); RestTemplate restTemplate = new RestTemplate(); MultiValueMap<String, String> body = new LinkedMultiValueMap<String, String>(); body.add("client_id", "my_client_id"); body.add("client_secret", "my_client_secret"); body.add("grant_type", "client_credentials"); HttpHeaders headers = new HttpHeaders(); headers.add("Accept", "application/json"); headers.set("Authorization", authHeader); HttpEntity<?> entity = new HttpEntity<Object>(body, headers); ResponseEntity<String> res = restTemplate.exchange( "https://api.assembla.com/token?", HttpMethod.POST, entity, String.class); System.out.println(res.getBody()); JSONObject json = JSONObject.fromObject(res.getBody()); String token = json.getString("access_token"); String url = "https://api.assembla.com/v1/spaces.json"; HttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(url); httpGet.addHeader("Accept", "application/json"); httpGet.addHeader("Content-type", "application/json"); httpGet.addHeader("Authorization", "Bearer " + token); BufferedReader bufferedReader = null; String jsonText = null; try { // httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpClient.execute(httpGet); bufferedReader = new BufferedReader(new InputStreamReader(response .getEntity().getContent())); String line = ""; while ((line = bufferedReader.readLine()) != null) { jsonText = line; } System.out.println(jsonText); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ArrayIndexOutOfBoundsException e) { e.printStackTrace(); } }
without basic auth the resttemplate get the access token but i want to implement the basic authentication with assembla plz give suggestion
Comment