다음 방법을 사용하여 보관 용 계정에 연결하려고합니다. 프로그램을 실행하면 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);
}
}
브라우저에서 URL을 열고 싶지 않습니다. 이것은 요구 사항입니다 –
그래서 나는 이것이 작동하는 방법이 아니라고 생각합니다. 이 API를 사용하려면 웹을 통해 앱을 승인 받아야합니다. –
나는 백업을하는 시스템을 가지고있다. 물론 브라우저를 열어서는 안되기 때문에 브라우저를 사용하여 나의 액세스 토큰을 생성하고 상수를 넣는 예제를 사용했다. 그것. 매번 다른 계정이 필요할 경우 문제가 될 수 있지만 내 요구 사항에 완벽하게 부합합니다. –