如何将dropbox的auth令牌直接输入java程序,以便用户不';我不需要复制粘贴它

如何将dropbox的auth令牌直接输入java程序,以便用户不';我不需要复制粘贴它,java,dropbox,dropbox-api,Java,Dropbox,Dropbox Api,我正在开发一个应用程序,我必须上传文件并从dropbox帐户下载文件,该应用程序是一个java桌面应用程序。我已经使用了示例代码,并且能够在没有任何故障的情况下运行它。但我想绕过用户需要将身份验证代码表单浏览器复制到应用程序的问题,如何才能做到这一点。我希望我的应用程序直接将auth令牌获取到应用程序中,因为这会给用户带来开销。 请各位专家帮帮我。 下面是我实现的代码 import com.dropbox.core.*; import java.awt.Desktop; import java

我正在开发一个应用程序,我必须上传文件并从dropbox帐户下载文件,该应用程序是一个java桌面应用程序。我已经使用了示例代码,并且能够在没有任何故障的情况下运行它。但我想绕过用户需要将身份验证代码表单浏览器复制到应用程序的问题,如何才能做到这一点。我希望我的应用程序直接将auth令牌获取到应用程序中,因为这会给用户带来开销。 请各位专家帮帮我。 下面是我实现的代码

import com.dropbox.core.*;

import java.awt.Desktop;
import java.io.*;
import java.util.Locale;

public class Main {
public static void main(String[] args) throws IOException, DbxException {
    // Get your app key and secret from the Dropbox developers website.
    final String APP_KEY = "xxxxxxxxxxxxxxxx";
    final String APP_SECRET = "xxxxxxxxxxxxxxxx";

    DbxAppInfo appInfo = new DbxAppInfo(APP_KEY, APP_SECRET);

    DbxRequestConfig config = new DbxRequestConfig("JavaTutorial/1.0",
        Locale.getDefault().toString());
    DbxWebAuthNoRedirect webAuth = new DbxWebAuthNoRedirect(config, appInfo);

    // Have the user sign in and authorize your app.
    String authorizeUrl = webAuth.start();
    System.out.println("1. Go to: " + authorizeUrl);
    System.out.println("2. Click \"Allow\" (you might have to log in first)");
    System.out.println("3. Copy the authorization code.");
    Desktop.getDesktop().browse(java.net.URI.create(authorizeUrl));
    String code = new BufferedReader(new InputStreamReader(System.in)).readLine().trim();

    // This will fail if the user enters an invalid authorization code.
    DbxAuthFinish authFinish = webAuth.finish(code);

    DbxClient client = new DbxClient(config, authFinish.accessToken);

    System.out.println("Linked account: " + client.getAccountInfo().displayName);

    File inputFile = new File("OpenCv_Video_display_link.txt");
    FileInputStream inputStream = new FileInputStream(inputFile);
    try {
        DbxEntry.File uploadedFile = client.uploadFile("/OpenCv_Video_display_link.txt",
            DbxWriteMode.add(), inputFile.length(), inputStream);
        System.out.println("Uploaded: " + uploadedFile.toString());
    } finally {
        inputStream.close();
    }

    DbxEntry.WithChildren listing = client.getMetadataWithChildren("/");
    System.out.println("Files in the root path:");
    for (DbxEntry child : listing.children) {
        System.out.println("    " + child.name + ": " + child.toString());
    }

    FileOutputStream outputStream = new FileOutputStream("121verbs.pdf");
    try {
        DbxEntry.File downloadedFile = client.getFile("/121verbs.pdf", null,
            outputStream);
        System.out.println("Metadata: " + downloadedFile.toString());
    } finally {
        outputStream.close();
    }
}
}

根据我在Android平台上的开发经验,您可以插入JavaScript代码来扫描网页并提取身份验证代码,如下所示:

@Override
public void onPageFinished(final WebView webView, final String url) {

    if (AUTHORIZE_SUBMIT_URL.equalsIgnoreCase(url)
            && AUTHORIZE_SUBMIT_TITLE.equalsIgnoreCase(webView.getTitle())) {

        webView.loadUrl("javascript:HtmlViewer.showHTML"
                + "('<html>'+document.getElementById('auth-code').innerHTML+'</html>');");
    }
}
@覆盖
公共void onPageFinished(最终WebView WebView,最终字符串url){
如果(授权提交URL.equalsIgnoreCase(URL)
&&授权\提交\标题.equalsIgnoreCase(webView.getTitle()){
loadUrl(“javascript:HtmlViewer.showHTML”
+“(''+document.getElementById('auth-code').innerHTML+'');”;
}
}

根据我在Android平台上的开发经验,您可以插入JavaScript代码来扫描网页并提取身份验证代码,如下所示:

@Override
public void onPageFinished(final WebView webView, final String url) {

    if (AUTHORIZE_SUBMIT_URL.equalsIgnoreCase(url)
            && AUTHORIZE_SUBMIT_TITLE.equalsIgnoreCase(webView.getTitle())) {

        webView.loadUrl("javascript:HtmlViewer.showHTML"
                + "('<html>'+document.getElementById('auth-code').innerHTML+'</html>');");
    }
}
@覆盖
公共void onPageFinished(最终WebView WebView,最终字符串url){
如果(授权提交URL.equalsIgnoreCase(URL)
&&授权\提交\标题.equalsIgnoreCase(webView.getTitle()){
loadUrl(“javascript:HtmlViewer.showHTML”
+“(''+document.getElementById('auth-code').innerHTML+'');”;
}
}
  • 点击应用程序。在Outh2下的这个页面上,查找生成的访问令牌
  • 单击Generate
  • 这是您的访问令牌。这将验证用户就是您,您不必每次都通过标准授权流
  • 我已经注释了不必要的代码,您只需要最后一行

    /* // Have the user sign in and authorize your app.
    String authorizeUrl = webAuth.start();
    System.out.println("1. Go to: " + authorizeUrl);
    System.out.println("2. Click \"Allow\" (you might have to log in first)");
    System.out.println("3. Copy the authorization code.");
    String code = new BufferedReader(new InputStreamReader(System.in)).readLine().trim();
    
    // This will fail if the user enters an invalid authorization code.
    DbxAuthFinish authFinish = webAuth.finish(code); */
    
    String accessToken = "Your access token goes here";
    
  • 点击应用程序。在Outh2下的这个页面上,查找生成的访问令牌
  • 单击Generate
  • 这是您的访问令牌。这将验证用户就是您,您不必每次都通过标准授权流
  • 我已经注释了不必要的代码,您只需要最后一行

    /* // Have the user sign in and authorize your app.
    String authorizeUrl = webAuth.start();
    System.out.println("1. Go to: " + authorizeUrl);
    System.out.println("2. Click \"Allow\" (you might have to log in first)");
    System.out.println("3. Copy the authorization code.");
    String code = new BufferedReader(new InputStreamReader(System.in)).readLine().trim();
    
    // This will fail if the user enters an invalid authorization code.
    DbxAuthFinish authFinish = webAuth.finish(code); */
    
    String accessToken = "Your access token goes here";
    
    基于这个例子,我用java制作了这个例子,它将打开一个Webview,在那里你可以引入你的dropbox凭据,dropbox将重定向你,重定向url将有令牌,然后我们从url获取令牌并打印到控制台

        public class AnotherClass extends Application {
        public static void main(String[] args) {
            java.net.CookieHandler.setDefault(new com.sun.webkit.network.CookieManager());
            launch(args);
        }
    
        //This can be any URL. But you have to register in your dropbox app
        final String redirectUri = "https://www.dropbox.com/1/oauth2/redirect_receiver";
        final String AppKey = "YOUR APP KEY ";
        final String url = "https://www.dropbox.com/1/oauth2/authorize?client_id=" + AppKey + "&response_type=token&redirect_uri=" + redirectUri;
    
    
        @Override
        public void start(Stage pStage) throws Exception {
    
            Stage primaryStage = pStage;
            primaryStage.setTitle("Dropbox Sign In");
    
    
            WebView webView = new WebView();
            WebEngine webEngine = webView.getEngine();
            webEngine.load(url);
            webEngine.locationProperty().addListener(new ChangeListener<String>() {
    
                @Override
                public void changed(ObservableValue<? extends String> arg0, String oldLocation, String newLocation) {
                    try {
                        if (newLocation.startsWith(redirectUri)) {
                            ArrayMap<String, String> map = parseQuery(newLocation.split("#")[1]);
                            if (map.containsKey("access_token")) {
                                System.out.print("Token: " + map.get("access_token"));
                            }
                        }
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                }
    
            });
    
            StackPane stackPane = new StackPane();
            stackPane.getChildren().add(webView);
    
            Scene rootScene = new Scene(stackPane);
    
            primaryStage.setScene(rootScene);
            primaryStage.show();
    
        }
    
        ArrayMap<String, String> parseQuery(String query) throws UnsupportedEncodingException {
            ArrayMap<String, String> params = new ArrayMap<String, String>();
            for (String param : query.split("&")) {
                String[] pair = param.split("=");
                String key = URLDecoder.decode(pair[0], "UTF-8");
                String value = URLDecoder.decode(pair[1], "UTF-8");
                params.put(key, value);
            }
            return params;
        }
    
    }
    
    公共类另一个类扩展应用程序{
    公共静态void main(字符串[]args){
    setDefault(新的com.sun.webkit.network.CookieManager());
    发射(args);
    }
    //这可以是任何URL。但您必须在dropbox应用程序中注册
    最终字符串重定向URI=”https://www.dropbox.com/1/oauth2/redirect_receiver";
    最后一个字符串AppKey=“您的应用程序密钥”;
    最终字符串url=”https://www.dropbox.com/1/oauth2/authorize?client_id=“+AppKey+”&response_type=token&redirect_uri=“+redirectUri;
    @凌驾
    public void start(Stage pStage)引发异常{
    阶段primaryStage=pStage;
    primaryStage.setTitle(“Dropbox登录”);
    WebView WebView=新建WebView();
    WebEngine WebEngine=webView.getEngine();
    加载(url);
    webEngine.locationProperty().addListener(新的ChangeListener()){
    @凌驾
    public void changed(observevalue基于此示例,我用java制作了此示例,它将打开一个Webview,您可以在其中引入dropbox凭据,dropbox将重定向您,重定向url将具有令牌,然后我们从url获取令牌并打印到控制台

        public class AnotherClass extends Application {
        public static void main(String[] args) {
            java.net.CookieHandler.setDefault(new com.sun.webkit.network.CookieManager());
            launch(args);
        }
    
        //This can be any URL. But you have to register in your dropbox app
        final String redirectUri = "https://www.dropbox.com/1/oauth2/redirect_receiver";
        final String AppKey = "YOUR APP KEY ";
        final String url = "https://www.dropbox.com/1/oauth2/authorize?client_id=" + AppKey + "&response_type=token&redirect_uri=" + redirectUri;
    
    
        @Override
        public void start(Stage pStage) throws Exception {
    
            Stage primaryStage = pStage;
            primaryStage.setTitle("Dropbox Sign In");
    
    
            WebView webView = new WebView();
            WebEngine webEngine = webView.getEngine();
            webEngine.load(url);
            webEngine.locationProperty().addListener(new ChangeListener<String>() {
    
                @Override
                public void changed(ObservableValue<? extends String> arg0, String oldLocation, String newLocation) {
                    try {
                        if (newLocation.startsWith(redirectUri)) {
                            ArrayMap<String, String> map = parseQuery(newLocation.split("#")[1]);
                            if (map.containsKey("access_token")) {
                                System.out.print("Token: " + map.get("access_token"));
                            }
                        }
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                }
    
            });
    
            StackPane stackPane = new StackPane();
            stackPane.getChildren().add(webView);
    
            Scene rootScene = new Scene(stackPane);
    
            primaryStage.setScene(rootScene);
            primaryStage.show();
    
        }
    
        ArrayMap<String, String> parseQuery(String query) throws UnsupportedEncodingException {
            ArrayMap<String, String> params = new ArrayMap<String, String>();
            for (String param : query.split("&")) {
                String[] pair = param.split("=");
                String key = URLDecoder.decode(pair[0], "UTF-8");
                String value = URLDecoder.decode(pair[1], "UTF-8");
                params.put(key, value);
            }
            return params;
        }
    
    }
    
    公共类另一个类扩展应用程序{
    公共静态void main(字符串[]args){
    setDefault(新的com.sun.webkit.network.CookieManager());
    发射(args);
    }
    //这可以是任何URL。但您必须在dropbox应用程序中注册
    最终字符串重定向URI=”https://www.dropbox.com/1/oauth2/redirect_receiver";
    最后一个字符串AppKey=“您的应用程序密钥”;
    最终字符串url=”https://www.dropbox.com/1/oauth2/authorize?client_id=“+AppKey+”&response_type=token&redirect_uri=“+redirectUri;
    @凌驾
    public void start(Stage pStage)引发异常{
    阶段primaryStage=pStage;
    primaryStage.setTitle(“Dropbox登录”);
    WebView WebView=新建WebView();
    WebEngine WebEngine=webView.getEngine();
    加载(url);
    webEngine.locationProperty().addListener(新的ChangeListener()){
    @凌驾
    
    public void已更改(observeValueWe在Dropbox开发者论坛上对此进行了一些讨论:。@smarx:我已经完成了讨论,正在尝试替换
    DbxWebAuthNoRedirect webAuth=new DbxWebAuthNoRedirect(config,appInfo);
    使用DbxWebAuth,但在`HttpSession session=request.getSession(true);`String sessionKey=“dropbox auth csrf token”`我无法添加此HttpSession,我尝试为其下载了不同的jar。请告诉我方法。我被卡住了。@smarx:当我键入此
    HttpSession session=request.getSession(true)时
    它向我显示了eclipse中的一个错误,即
    请求无法解析
    DbxWebAuth
    仅适用于web应用。
    请求
    需要是对HTTP请求的引用。@smarx:那么我如何在我的桌面应用程序中使用DbxWebAuth绕过网页中的auth令牌复制粘贴。我们在Dropbox开发者论坛关于此:。@smarx:我已经完成了讨论,正在尝试替换
    DbxWebAuthNoRedirect webAuth=new DbxWebAuthNoRedirect(config,appInfo