2014-04-04 76 views
1

我想連接到Dropbox使用下面的方法。 當我運行該程序時,它是否同時提供請求代碼和響應代碼200,無論USER_NAME和PASSWORD是正確還是錯誤,爲什麼?Dropbox身份驗證

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


    } 

} 

回答

0

您不應該通過代碼打開authorizeUrl。相反,請在瀏覽器中打開並授權此應用使用您的帳戶。

它每次都返回狀態200(確定),因爲找到了登錄頁面,要求您進行登錄或(如果已經登錄)授權您的應用程序。

如果您計劃使用戶能夠做到這一點在您的網站,該教程演示:

在手的授權URL,我們現在可以要求用戶授權您的應用程序。爲了避免在本教程中設置Web服務器的麻煩,我們只是打印URL並要求用戶按Enter鍵以確認他們已授權您的應用程序。

但是,在真實世界的應用程序中,您需要自動將用戶發送到授權URL並傳入回調URL,以便用戶按下按鈕後可以無縫地重定向回您的應用程序。

來源:Using the Core API in Java

要繼續教程,簡單的複製粘貼&網址在瀏覽器上,就批准,然後你會得到授權碼進程繼續進行。

+0

我不想在瀏覽器中打開一個URL。這是要求 –

+0

所以我認爲這不是工作的方式。此API要求應用程序通過網站進行授權。 –

+0

我有一個系統做備份,當然它不需要瀏覽器打開,所以我使用這個例子來生成我的訪問令牌(使用瀏覽器)並放入一個常量,之後我只需要使用它。如果我每次都需要不同的帳戶,這將會是一個問題,但它完全適合我的需求。 –