2014-04-04 3 views
1

다음 방법을 사용하여 보관 용 계정에 연결하려고합니다. 프로그램을 실행하면 USER_NAME과 PASSWORD가 맞는지 틀린 지 여부와 상관없이 요청 코드와 응답 코드가 모두 제공됩니다. 그 이유는 무엇입니까?보관 용 계정

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 (OK) 상태로 돌아오고 로그인하거나 이미 로그인 한 경우 앱을 승인해야합니다. 당신이 당신의 웹 사이트에이 작업을 수행 할 수 있도록하려는 경우

, 튜토리얼을 보여줍니다

손에 인증 URL로

, 우리가 지금 당신의 응용 프로그램에 권한을 부여하도록 사용자에게 요청할 수 있습니다. 이 자습서에서 웹 서버를 설정하는 번거 로움을 피하기 위해 URL을 인쇄하고 사용자에게 Enter 키를 눌러 앱을 승인했음을 확인하는 것뿐입니다.

그러나 실제 앱에서는 자동으로 사용자를 승인 URL로 보내고 콜백 URL을 전달하여 버튼을 누른 후 사용자가 앱으로 원활하게 리디렉션되도록하는 것이 좋습니다.

출처 : Using the Core API in Java

이 자습서를 계속하려면, 단순히 허가 않는 브라우저에 & 붙여 넣기 URL을 복사 한 다음 프로세스를 계속하려면 인증 코드를 얻을 것이다.

+0

브라우저에서 URL을 열고 싶지 않습니다. 이것은 요구 사항입니다 –

+0

그래서 나는 이것이 작동하는 방법이 아니라고 생각합니다. 이 API를 사용하려면 웹을 통해 앱을 승인 받아야합니다. –

+0

나는 백업을하는 시스템을 가지고있다. 물론 브라우저를 열어서는 안되기 때문에 브라우저를 사용하여 나의 액세스 토큰을 생성하고 상수를 넣는 예제를 사용했다. 그것. 매번 다른 계정이 필요할 경우 문제가 될 수 있지만 내 요구 사항에 완벽하게 부합합니다. –

관련 문제