Dropbox身份验证

时间:2014-04-04 12:05:19

标签: java authentication dropbox connect

我正在尝试使用以下方法连接到Dropbox。 当我运行程序时,无论USER_NAME和PASSWORD是正确还是错误,它都会提供请求代码和响应代码200,为什么?

import com.dropbox.core.*;

import java.awt.Desktop;
import java.io.*;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.util.Locale;

import sun.misc.BASE64Encoder;

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 = "";
        final String APP_SECRET = "";
        final String USER_NAME= "";
        final String PASSWORD= ";
        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.");

        URL obj = new URL(authorizeUrl);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        con.setRequestMethod("GET");
        int responseCode = con.getResponseCode();
        System.out.println("\nSending 'GET' request to URL : " + authorizeUrl);
        System.out.println("Response Code : " + responseCode);
         URL url = new URL(authorizeUrl);
         HttpURLConnection req = (HttpURLConnection)url.openConnection();
         BASE64Encoder enc = new sun.misc.BASE64Encoder();
          String userpassword = USER_NAME + ":" + PASSWORD;
          String encodedAuthorization = enc.encode( userpassword.getBytes() );
          req.setRequestProperty("Authorization", "Basic "+
                encodedAuthorization);
          int reqCode = req.getResponseCode();
          System.out.println("request code"+reqCode);
//      BufferedReader in = new BufferedReader(
//              new InputStreamReader(con.getInputStream()));
//      String inputLine;
//      StringBuffer response = new StringBuffer();
// 
//      while ((inputLine = in.readLine()) != null) {
//          response.append(inputLine);
//      }
//      in.close();
// 
//      //print result
//      System.out.println(response.toString());
       // Desktop.getDesktop().browse(java.net.URI.create(authorizeUrl));
//      Authenticator.setDefault (new Authenticator() {
//          protected PasswordAuthentication getPasswordAuthentication() {
//              return new PasswordAuthentication ("USER_NAME", "PASSWORD".toCharArray());
//          }
//      });


        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);


    }

}

1 个答案:

答案 0 :(得分:0)

您不应该按代码打开authorizeUrl。相反,请在浏览器中打开并授权此应用使用您的帐户。

每次返回状态200(OK),因为找到了登录页面,要求您进行登录,或者如果已经登录,则授权您的应用程序。

如果您计划让用户在您的网站上执行此操作,则教程将演示:

  

使用授权网址,我们现在可以要求用户授权您的应用。为避免在本教程中设置网络服务器的麻烦,我们只是打印网址并要求用户按Enter键确认他们已授权您的应用。

     

但是,在实际应用中,您需要自动将用户发送到授权网址并传入回调网址,以便在按下按钮后将用户无缝重定向回应用。

     

来源:Using the Core API in Java

要继续教程,只需复制&在您的浏览器上粘贴网址,进行授权,然后您将获得授权码以继续此过程。