繁体   English   中英

Java Dropbox HttpServletRequest(java.lang.UnsupportedOperationException:尚不支持。)

[英]Java Dropbox HttpServletRequest ( java.lang.UnsupportedOperationException: Not supported yet.)

在我的 java SWING 应用程序中,用户可以将文件传输到他的 DropBox 空间,我已经成功实现了该库,我正在尝试获取用户的访问令牌,而无需将其复制并粘贴到我的应用程序中。 根据文档,我必须使用 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);

笔记

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

这是一种基于 heroku 身份验证和谷歌容器注册表身份验证工作方式的建议方法。

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

基本理念

开发一个 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 登录信息,输入其凭据并接受同意页面。
  • 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 功能。

暂无
暂无

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

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