【问题标题】:Java Dropbox HttpServletRequest ( java.lang.UnsupportedOperationException: Not supported yet.)Java Dropbox HttpServletRequest ( java.lang.UnsupportedOperationException: 尚不支持。)
【发布时间】:2020-11-30 11:35:27
【问题描述】:

在我的 java SWING 应用程序中,用户可以将文件传输到他的 DropBox 空间,我已经成功实现了该库,并且我正在尝试获取用户的访问令牌,而无需将其复制并粘贴到我的应用程序中。 根据documentation,我必须使用HttpServletRequest才能得到我想要的,但是我得到了一个异常,正如标题所写的那样。

public class LoginServlet implements HttpServletRequest {

protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {

    // read form fields
    String token = request.getParameter("data-token");
 
 
    // get response writer
    PrintWriter writer = response.getWriter();

    // build HTML code
    String htmlRespone = "<html>";
    htmlRespone += "<h2>token: " + token + "<br/>";
    htmlRespone += "</html>";

    // return response
    writer.println(htmlRespone);

    response.sendRedirect(Main.redirectUri);
    

}
 @Override
public HttpSession getSession(boolean bln) {      
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public HttpSession getSession() {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

在主类中

 LoginServlet request = new LoginServlet();
       
        // Fetch the session to verify our CSRF token
        HttpSession session = request.getSession(true);
        String sessionKey = "dropbox-auth-csrf-token";
        DbxSessionStore csrfTokenStore = new DbxStandardSessionStore(session, sessionKey);

        DbxRequestConfig config = new DbxRequestConfig("User"); //Client name can be whatever you like
        DbxAppInfo appInfo = new DbxAppInfo(App key, App secret);
        DbxWebAuth webAuth = new DbxWebAuth(config, appInfo);
        DbxWebAuth.Request authRequest = DbxWebAuth.newRequestBuilder()
                .withRedirectUri(redirectUri, csrfTokenStore)
                .build();

        String url = webAuth.authorize(authRequest);

        Desktop.getDesktop().browse(new URL(url).toURI());

        DbxAuthFinish authFinish;
        try {
            authFinish = webAuth.finishFromRedirect(redirectUri, csrfTokenStore, request.getParameterMap());
        } catch (DbxWebAuth.BadRequestException ex) {
            //log("On /dropbox-auth-finish: Bad request: " + ex.getMessage());
            //response.sendError(400);
            return;
        } catch (DbxWebAuth.BadStateException ex) {
            // Send them back to the start of the auth flow.
            //response.sendRedirect(redirectUri);
            return;
        } catch (DbxWebAuth.CsrfException ex) {
            //log("On /dropbox-auth-finish: CSRF mismatch: " + ex.getMessage());
            //response.sendError(403, "Forbidden.");
            return;
        } catch (DbxWebAuth.NotApprovedException ex) {
            // When Dropbox asked "Do you want to allow this app to access your
            // Dropbox account?", the user clicked "No".

            return;
        } catch (DbxWebAuth.ProviderException ex) {
            //log("On /dropbox-auth-finish: Auth failed: " + ex.getMessage());
            //response.sendError(503, "Error communicating with Dropbox.");
            return;
        } catch (DbxException ex) {
            //log("On /dropbox-auth-finish: Error getting token: " + ex.getMessage());
            //response.sendError(503, "Error communicating with Dropbox.");
            return;
        }
        String accessToken = authFinish.getAccessToken();

        // Save the access token somewhere (probably in your database) so you
        // don't need to send the user through the authorization process again.
        // Now use the access token to make Dropbox API calls.
        DbxClientV2 client = new DbxClientV2(config, accessToken);
        //...

全栈

    java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:873)
Caused by: java.lang.UnsupportedOperationException: Not supported yet.
    at cloud.LoginServlet.getSession(LoginServlet.java:404)
    at cloud_new.Main.main(Main.java:103)

at cloud_new.Main.main(Main.java:103) 这是 HttpSession session = request.getSession(true);

【问题讨论】:

  • 能否包含完整的异常堆栈?
  • HttpServletRequest 是从 Dropbox 向应用程序需要提供的 Servlet 的请求。您似乎在扩展 HttpServletRequest 但实现了一个 Servlet。您看到的异常是从您自己的代码中抛出的。
  • 所以你重启了赏金?你还不明白,你想做的事是不可能的?
  • @Olivier 所以这个 (dropbox.github.io/dropbox-sdk-java/api-docs/v2.0.x/com/dropbox/…) 对你来说是假的????我见过周围的 Java 应用程序这样做!!
  • 该文档是关于 Web 应用程序的,而不是 Swing 应用程序(我在之前的评论中已经说过,你删除了,顺便说一句)。

标签: java servlets


【解决方案1】:

注意

这不是解决与某些 http 错误相关的主要 java 问题的解决方案。

这是一种基于 Heroku 身份验证和 google 容器注册表身份验证工作原理的建议方法。

而且dropbox等平台也没有提供一种特殊的admin api_key来消耗最终的用户资源。由于需要在真实浏览器中进行 Web 登录,并且这不能用 web_views 或 android 或 ios 中的内部浏览器轻松替换或模拟。 Source

基本思路

开发一个 Web 应用程序,该应用程序公开两个功能:接收来自 Dropbox 授权平台的重定向和一个用于查询用户是否已通过身份验证的休息端点。

假设

  • 名为 my-oauth2-helper.com 的网站

流程

  • 用户启动桌面应用程序。
  • my-oauth2-helper.com/validate/auth发送用户电子邮件的桌面应用程序查询。
  • my-oauth2-helper.com/validate/auth 返回一个字段,表明用户没有有效的身份验证和其他字段与保管箱登录 url。
  • dropbox 登录 url,其中包含之前在 Dropbox 开发者控制台中配置的经典 oauth2 字段:client_id、redirect_uri 等。
  • 桌面应用程序使用此登录 URL 打开浏览器选项卡。
  • 系统会提示用户使用有效的 Dropbox Web 登录名,输入其凭据并接受 consent 页面。
  • Dropbox 遵循 oauth2 协议,将用户重定向到 my-oauth2-helper.com/redirect 是经典字段redirect_uri
  • my-oauth2-helper.com/redirect 接收 auth_code 并将其交换为该用户的有效 access_token 并将其存储在一种数据库中。之后可能会显示如下消息:“现在,您可以关闭此页面并返回桌面应用程序”
  • 由于提示用户使用 Dropbox Web 登录,桌面应用程序开始定期查询用户是否具有对端点 my-oauth2-helper.com/token 的有效身份验证,并将电子邮件作为请求参数发送
  • 在生成有效的access_token 之后,my-oauth2-helper.com/token 端点为此用户返回一个有效的 access_token。
  • access_token 已准备就绪,可以使用用户在同意页面中接受的任何 Dropbox api 功能。

【讨论】:

    猜你喜欢
    • 2011-06-19
    • 1970-01-01
    • 1970-01-01
    • 2014-11-15
    • 2013-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多