BoxAPIConnection wrong Parameters Java (Error 400)
So I have a function to donwload files from a folder and subfolders and when I use it with the developer token it works perfectly fine.
BoxAPIConnection api = new BoxAPIConnection("DEV TOKEN");
I also have the authorization done which provides me with an access token but I can't quite fit both together.
What does the BoxAPIConnection require to work? What's the formatting? If I just use the accesstoken instead of the dev token I receive an API Error 400. In the library, there are different formats like BoxAPIConnection(String), BoxAPIConnection(String, String), BoxAPIConnection(String, String, String) but I am not entirely sure what I need because the documentation seems to be lacking. What strings do I need and in which order do I need them to get the BoxAPIConnection up and running? Client ID? Client Secret? Access Token?
Kind regards
-
There's multiple ways to Rome, but in the end the Box API always requires a token to connect. I'm not familiar with the Java SDK so I can't answer what the string arguments mean.
The developer token you used up until here is a token for testing, but usually you need to request these access tokens through OAuth2 or JWT. I'm thinking you are looking for JWT authentication. Take a look here:
https://developer.box.com/docs/authenticate-with-jwt
There's the steps for Java SDK.
If you want a user to be prompted to authenticate that your program should be allowed to access their account then you're looking for OAuth2:
https://developer.box.com/docs/authenticate-with-oauth-2
-
I've already implemented authentication, I receive an access token and a basic test request is working.
The problem is that I'm not sure on how to use this exact access token with my download function.
My download function uses the BoxAPIConnection and I don't know what arguments I need or in which order I need them.
-
Hi bkhane,
The Java SDK includes a method for downloading content with a BoxAPIConnection. This involves instantiating a BoxFile instance with your BoxAPIConnection and then calling the BoxFile's download() method. I would also suggest reviewing the BoxFiles doc here.
-
private static void getIDs(String root, String devToken) { BoxAPIConnection api = new BoxAPIConnection(devToken); // DEV TOKEN BoxFolder folder = new BoxFolder(api, root); for (BoxItem.Info itemInfo : folder) { if (itemInfo instanceof BoxFile.Info) { BoxFile.Info fileInfo = (BoxFile.Info) itemInfo; //Datei Informationen wiedergeben System.out.println("FileID: " + fileInfo.getID() + " Name: " + fileInfo.getName()); String fileID = fileInfo.getID(); BoxFile file = new BoxFile(api, fileID); BoxFile.Info info = file.getInfo(); try { //Dateien in dirName speichern String dirName = "F:\\eclipse\\khjbgl\\Downloaded Files\\"; FileOutputStream stream = new FileOutputStream(dirName + info.getName()); file.download(stream); stream.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } // System.out.print(fileInfo); // folder information } else if (itemInfo instanceof BoxFolder.Info) { BoxFolder.Info folderInfo = (BoxFolder.Info) itemInfo; System.out.println("FolderID: " + folderInfo.getID() + " Name: " + folderInfo.getName()); // System.out.print(folderInfo); // Do something with the folder. getIDs(folderInfo.getID(), devToken); } }
the function in itself is working perfectly! BUT I don't know which parameter I should give to the BoxAPIConnection. Currently, it is running on the dev token but if I exchange that token through an access token then it is not working anymore! instantiating the BoxAPIConnection is the problem!
-
In general, there's some Java SDK-specific documentation around authentication that you may find helpful. There are several different ways of configuring the SDK authentication, but these are probably the most common:
- If you have an access token, and just want your code to run with that token (and never need to get a new token automatically), for example with a Developer Token, you can use the BoxAPIConnection(String token) constructor
- If you're using 3-legged OAuth2 and expect a user to enter their own login and password to authenticate with your application, you'll probably want to use either BoxAPIConnection(String clientID, String clientSecret, String authCode) or BoxAPIConnection(String clientID, String clientSecret, String accessToken, String refreshToken) depending on how you're handling the OAuth2 flow in your application
- If you're using Server-to-Server auth with JWTs, you'll need to BoxDeveloperEditionAPIConnection.getAppEnterpriseConnection(BoxConfig config) to construct the API connection from the JSON config file you can download from the Developer Console for that type of application
Please sign in to leave a comment.
Comments
5 comments