how to implement basic authentication with resttemplate for assembla

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rockmahee
    New Member
    • May 2014
    • 1

    how to implement basic authentication with resttemplate for assembla

    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();
        }
    }
    this code have an error: 400 Bad Request when the request for token is posted and due to this with out access token i cant get my response.

    without basic auth the resttemplate get the access token but i want to implement the basic authentication with assembla plz give suggestion
    Last edited by Rabbit; May 21 '14, 07:17 PM. Reason: Please use [CODE] [/CODE] tags when posting code or formatted data.
  • rules engine
    New Member
    • Jun 2014
    • 15

    #2
    Has server implemented processing of
    client_id and client_secret?

    Comment

    • sudheer2845
      New Member
      • May 2015
      • 1

      #3
      i am also working on same issue yaar...if anybody knows post here

      Comment

      Working...