0
votes

I have written the following java code to create a group or box enterprise user. Each time, it fails with the 400. With a lot of trials in the JSON Object input format (more double-quotes,single-quotes,escape character,etc) , I have managed to get error codes right from 400,403,404 and 409. In the process, I have also tried using a string as a json object format.

Lastly, I have created a json object and sent it across to the server using HttpUrlConnection. But still no luck. With a similar code and access token, I have been able to get users/groups/collaborations. But never been able to create the user/group through code. I also tried curl command line and there too I was stuck. My best achievement was that I could get a 403 error using curl command indicating a forbidden or insufficient rights. However, please note that I have generated this access token through enterprise box admin credentials and able to add the user/group through box UI. Also, through code using the above access token I have been able to perform GET. Can someone please point out what I am missing in the code below??

URL url = new URI("https", "www.box.com", "/api/2.0/groups", null).toURL();
JSONObject jobj = new JSONObject();
jobj.put("name", "abcdef1");
HttpURLConnection urlConn = (HttpURLConnection)url.openConnection();
urlConn.setDoOutput(true);
urlConn.setDoInput(true);
urlConn.setRequestMethod(method)
urlConn.setRequestProperty("Authorization", "Bearer " + accessToken);
urlConn.setRequestProperty("Content-Type", "application/json");         
DataOutputStream wr = new DataOutputStream(urlConn.getOutputStream());
wr.writeUTF(URLEncoder.encode((jobj.toJSONString(),"UTF-8"));
wr.flush();
wr.close();
if (urlConn.getResponseCode() == 200)
{
 //Read buffer    
}
else
    throw new Exception("Reaches HERE!!!!");                    
1

1 Answers

0
votes

Box applications using the OAuth 2.0 workflow now support two scopes of accounts access:

1. Read and write all files and folders
2. Manage an enterprise

The second option -- which seems related to what you're trying to do -- is disabled by default. Have you enabled it for your application?