Getting Error 400 while Requesting an Access token (Java)
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.
-
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.
Please sign in to leave a comment.
Comments
8 comments