簡體   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