0

I am trying to fetch current user info after making him log into his box, using the box sdk for android. In the box api documentation, they have mentioned everything using curls. I am not familiar with curl. So, can anyone please give me a java equivalent for this curl operation :

curl https://api.box.com/2.0/users/me-H "Authorization: Bearer ACCESS_TOKEN". 

I have the users access token.So, please give me a java equivalent for the above curl operation.

dora
  • 2,047
  • 3
  • 18
  • 20

2 Answers2

0

You can use java HttpClient

HttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(URL);

httpPost.setHeader("Authorization", "Bearer ACCESS_TOKEN"); // add headers if needded

//set params
BasicNameValuePair[] params = new BasicNameValuePair[] {new BasicNameValuePair("param1","param value"),
            new BasicNameValuePair("param2","param value")};

UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity( Arrays.asList(params), "utf-8");
    httpPost.setEntity(urlEncodedFormEntity);

//execute request
HttpResponse response = client.execute(httpPost);
HttpEntity entity = response.getEntity();

String body = EntityUtils.toString(entity); //here response in string you can parse it
Sergey Pekar
  • 8,555
  • 7
  • 47
  • 54
  • what do i need to put for this line : -H "Authorization: Bearer ACCESS_TOKEN" and how?. can you please tell the exact equivalent java code for the above curl code . i have the access token.but how to use it? – dora Mar 13 '14 at 19:19
  • You should add it as header like that httpPost.setHeader("Authorization", "Bearer ACCESS_TOKEN"); – Sergey Pekar Mar 13 '14 at 19:21
  • i cant test your answer right away, bcoz i am away from my work computer.I will test it tomorrow and vote you up, if your answer helps.Thanks!!!!!! – dora Mar 13 '14 at 19:23
  • Pekar: But FYI, i have tried it the same way in hurl it, But it didnt work! – dora Mar 13 '14 at 19:24
  • @Pekar: in hurl i got this reply "error="invalid_request", error_description="The access token was not found."" even though i sent a very valid access token. – dora Mar 13 '14 at 19:30
  • Sorry you need to parse Www-Authenticate header to see why authorization information. Call response.getFirstHeader("Www-Authenticate") to get this header – Sergey Pekar Mar 13 '14 at 19:31
0

The HttpClient object that the other answer proposes is now deprecated. I solved a CURL problem like yours (with the extra difficulty of uploading a .wav file). Check out my code in this this answer/question. How to upload a WAV file using URLConnection

Community
  • 1
  • 1
Josh
  • 6,251
  • 2
  • 46
  • 73