简体   繁体   English

授权后如何使用API​​连接? Java的

[英]How do I use an API Connection after the authorization? Java

Technically speaking I have the authorization running and my program can use the created BoxAPIConnection to download/upload from the box cloud. 从技术上讲,我正在运行授权,并且我的程序可以使用创建的BoxAPIConnection从盒云下载/上传。 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. 现在是实际的问题:如果我在其他任何地方调用下载功能,则无法访问在登录过程中创建的BoxAPIConnection api。 The api is locally in the authentication part, but I also need it in other functions. api在身份验证部分中位于本地,但在其他功能中也需要它。 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. 我尝试将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);
}

How do I use the created api in my other functions without prompting the user to authorize the entire software again? 如何在其他功能中使用创建的api,而又不提示用户再次授权整个软件?

Create a JVM level Cache(HashMap) and store the apiconnection in it. 创建一个JVM级别的Cache(HashMap)并将apiconnection存储在其中。 After saving it, use that apiconnection all through till it expires. 保存它之后,请一直使用该apiconnection直至其过期。 After the connection expires in an hr(approx), delete the old api from cache and save the new one. 连接在hr(大约)后过期后,从缓存中删除旧的api并保存新的api。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM