Join BoxWorks in San Francisco Nov 12-13! Keynotes, product demos, and Box Master Classes. Reserve your spot!

BoxAPIConnection wrong Parameters Java (Error 400)

New post

Comments

5 comments

  • RobinDeSchepper

    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

     

    0
    Comment actions Permalink
  • bkhane

    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.

    0
    Comment actions Permalink
  • mbehn-box

    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.

     

     

    0
    Comment actions Permalink
  • bkhane
    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!

    0
    Comment actions Permalink
  • mwiller

     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:

     

    1. 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
    2. 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
    3. 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
    0
    Comment actions Permalink

Please sign in to leave a comment.