我正在尝试使用以下方法连接到 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);
}
}
您不应该通过代码打开authorizeUrl。相反,请在浏览器中打开并授权此应用程序使用您的帐户。
每次都会返回状态 200(正常),因为找到了登录页面,要求您进行登录,或者如果已经登录,则授权您的应用程序。
如果您计划让用户在您的网站上执行此操作,本教程将演示:
有了授权 URL,我们现在可以要求用户授权您的应用程序。为了避免在本教程中设置 Web 服务器的麻烦,我们只是打印 URL 并要求用户按 Enter 键以确认他们已授权您的应用程序。
但是,在现实应用程序中,您需要自动将用户发送到授权 URL 并传入回调 URL,以便用户在按下按钮后无缝重定向回您的应用程序。
要继续您的教程,只需在浏览器上复制并粘贴 URL,进行授权,然后您将获得授权代码以继续该过程。