Technically speaking I have the authorization running and my program can use the created BoxAPIConnection to download/upload from the box cloud. But I can only get it to work within this part:
public void loginButtonClicked() throws IOException, URISyntaxException
{
if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
Desktop.getDesktop().browse(new URI("http://localhost:4567/start"));
}
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 authentication code
String code = req.queryParams("code");
// Instantiate new Box API connection object
BoxAPIConnection api = new BoxAPIConnection(ConfigAuth.client_id,ConfigAuth.client_secret,code);
String rootID = "0";
String targetDir = "F:\\eclipse\\khjbgl\\Downloaded Files\\";
DownloadAll(rootID, targetDir, api);
return "display page";
});
THe button is only for logging in and authorizing the Software, so it is not supposed to immediately download anything. Now the actual problem: If I call the download function anywhere else, then I'm unable to acess the BoxAPIConnection api created in the login process. The api is locally in the authentication part, but I also need it in other functions. I tried saving the state of the api Connection in a file and then restoring it in my download function but this gives me a json.ParseException.
public void loginButtonClicked() throws IOException, URISyntaxException
{
if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
Desktop.getDesktop().browse(new URI("http://localhost:4567/start"));
}
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 authentication code
String code = req.queryParams("code");
// Instantiate new Box API connection object
BoxAPIConnection api = new BoxAPIConnection(ConfigAuth.client_id,ConfigAuth.client_secret,code);
JSONParser parser = new JSONParser();
JSONObject obj = (JSONObject) parser.parse(api.save());
FileWriter file = new FileWriter("API.txt");
try {
file.write(obj.toJSONString());
System.out.println("successfully copied json object to file");
System.out.println("\nJSON Object: " + obj);
} catch (IOException e)
{
e.printStackTrace();
}
finally {
file.flush();
file.close();
}
//String rootID = "0";
//String targetDir = "F:\\eclipse\\khjbgl\\Downloaded Files\\";
//DownloadAll(rootID, targetDir, api);
return "display page";
});
}
public void DownloadAllClick (ActionEvent event) throws FileNotFoundException, IOException {
File file = new File ("API.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
StringBuilder sb = new StringBuilder();
String line = null;
String ls = System.getProperty("line.seperator");
while ((line = br.readLine()) != null) {
sb.append(line);
sb.append(ls);
}
//sb.deleteCharAt(sb.length() -1);
br.close();
String apiString = sb.toString();
BoxAPIConnection api = BoxAPIConnection.restore(ConfigAuth.client_id, ConfigAuth.client_secret, apiString);
String rootID = "0";
String targetDir = "F:\\eclipse\\khjbgl\\Downloaded Files\\";
DownloadAll(rootID, targetDir, api);
}
How do I use the created api in my other functions without prompting the user to authorize the entire software again?
Create a JVM level Cache(HashMap) and store the apiconnection in it. After saving it, use that apiconnection all through till it expires. After the connection expires in an hr(approx), delete the old api from cache and save the new one.