从技术上讲,我正在运行授权,我的程序可以使用创建的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连接的状态保存到文件中,然后在下载功能中恢复它,但这给了我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,而又不提示用户再次授权整个软件?
答案 0 :(得分:0)
创建一个JVM级别的Cache(HashMap)并将apiconnection存储在其中。保存它之后,请一直使用该apiconnection直至其过期。 连接在hr(大约)后过期后,从缓存中删除旧的api并保存新的api。