从技术上讲,我已经运行授权,我的程序可以使用创建的BoxAPIConnection从盒云下载/上传。但我只能在这部分内工作:
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";
});
该按钮仅用于登录和授权软件,因此不应立即下载任何内容。现在实际问题:如果我在其他任何地方调用下载函数,那么我无法访问登录过程中创建的BoxAPIConnection api。 api在认证部分是本地的,但我在其他功能中也需要它。我尝试在一个文件中保存api Connection的状态,然后在我的下载函数中恢复它,但这给了我一个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);
}
如何在不提示用户再次授权整个软件的情况下在其他功能中使用创建的api?
创建一个JVM级别缓存(HashMap)并将apiconnection存储在其中。保存后,使用该连接直到它过期。在连接以小时(大约)到期后,从缓存中删除旧的api并保存新的api。