Skip to main content

In the last step of the OAuth2 I'm getting an error 400, so my http request is probably not formatted properly. 

I'm using java so I tried to turn the Curl command from the official documentation  into an HttpPostRequest.

(Request access token: https://developer.box.com/reference#token)

curl https://api.box.com/oauth2/token \
-d 'grant_type=authorization_code' \
-d 'code=' \
-d 'client_id=' \
-d 'client_secret=' \
-X POST

Thats my current code for it and I'm not sure where my Mistake is..

HttpClient httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost(url);


List params = new ArrayList(2);
params.add(new BasicNameValuePair("grant_type", "authorization_code"));
params.add(new BasicNameValuePair("code",code));
params.add(new BasicNameValuePair("client_id", ConfigAuth.client_id));
params.add(new BasicNameValuePair("client_secret", ConfigAuth.client_secret));
httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));

//Execute and get the response.
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
System.out.println(response);

Long story short, I get Error 400 and I don't understand why. 


URL obj = new URL(url);
HttpsURLConnection connection = (HttpsURLConnection) obj.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("grant_type=","authorization_code");
connection.setRequestProperty("code", code);
connection.setRequestProperty("client_id", ConfigAuth.client_id);
connection.setRequestProperty("client_secret", ConfigAuth.client_secret);
connection.setRequestProperty("Content-Length", "100");
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response code:" + connection.getResponseCode());
System.out.println("Response message:" + connection.getResponseMessage());
// Read the response:
BufferedReader reader = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String line;
StringBuffer response = new StringBuffer();
while ((line = reader.readLine()) != null) {
response.append(line);
}
reader.close();
System.out.println(response.toString());

 this is my second approach. Now I get Error code 411 and not 400. Any Ideas on how to fix that? I tried adding 

connection.SetRequestProperty("Content-Length", "0"); but that didn't do any good either. 



 hey there, have you considered using our SDK? A guide is available for it here.


 


https://developer.box.com/docs/authenticate-with-oauth-2



I've tried that but that didn't seem to work. 

BoxAPIConnection works with a developer token, but if I use client ID, Secret and the authorization token then it is not creating a working connection for me. So I tried requesting an actual access token so I could create a working BoxAPIConnection. If I execute the following I get an Error 400. That's why I tried a different approach. I really could use some help!

 get("/start", (req, res) -> {



// Redirect user to login with Box
String box_redirect = ConfigAuth.box_redirect
+ "?response_type=code"
+ "&client_id=" + ConfigAuth.client_id
+ "&client_secret=" + ConfigAuth.client_secret
+ "&redirect_uri=" + ConfigAuth.redirect_uri;
res.redirect(box_redirect);
return "redirecting...";





});


get("/return", (req, res) -> {
// Capture auth code
String code = req.queryParams("code");

System.out.println(code);
BoxAPIConnection client = new BoxAPIConnection(ConfigAuth.client_id,ConfigAuth.client_secret,code);


BoxFolder rootFolder = BoxFolder.getRootFolder(client);
FileInputStream stream = new FileInputStream(ConfigAuth.file_path);
BoxFile.Info fileinfo = rootFolder.uploadFile(stream, "tax.txt");
stream.close();
return "Display Page";

});

 



 would you be able to provide the full body error response? I'm looking for the Box request ID so I can take a look at our backend logs. Alternatively, you can open a support case with all the details so we can help you further investigate. 



I've opened a ticket with my client ID but I'm not sure where I can find the box Request ID.



 no worries! what is the ticket number? I wanna grab it so you can work with me directly. If you could also provide an example date/time/timezone you received the error that'd be most helpful! 



My ticket number: 1813690

Last time I got the error was 7.42 3rd of January 2018 (central European time)



 thank you! Let me do some digging and I'll follow up with you in the ticket. 



Reply