Java Dropbox身份验证
我正在尝试使用以下方法连接到Dropbox。 当我运行程序时,它会给出请求代码和响应代码200,无论用户名和密码是正确的还是错误的,为什么Java Dropbox身份验证,java,authentication,dropbox,connect,Java,Authentication,Dropbox,Connect,我正在尝试使用以下方法连接到Dropbox。 当我运行程序时,它会给出请求代码和响应代码200,无论用户名和密码是正确的还是错误的,为什么 import com.dropbox.core.*; import java.awt.Desktop; import java.io.*; import java.net.Authenticator; import java.net.HttpURLConnection; import java.net.PasswordAuthentication; imp
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。相反,请在浏览器中打开并授权此应用程序使用您的帐户 它每次都返回状态200 OK,因为找到了登录页面,要求您进行登录,或者,如果已经登录,则授权您的应用程序 如果您计划让用户在您的网站上执行此操作,本教程将演示: 有了授权URL,我们现在可以要求用户授权您的应用程序。为了避免在本教程中设置web服务器的麻烦,我们只需打印URL并要求用户按Enter键确认他们已授权您的应用程序 但是,在现实世界的应用程序中,您需要自动将用户发送到授权URL并传入回调URL,以便用户在按下按钮后无缝重定向回您的应用程序 资料来源:
要继续您的教程,只需在浏览器上复制并粘贴URL,进行授权,然后您将获得授权代码以继续此过程。我不想在浏览器中打开URL。这是要求,所以我认为这不是工作的方式。此API要求应用程序通过web进行授权。我有一个进行备份的系统,当然它不需要打开浏览器,因此我使用该示例使用浏览器生成我的访问令牌并输入一个常量,之后我只需使用它。如果我每次都需要不同的帐户,这将是一个问题,但它完全满足了我的需要。是否可以通过编程方式实现?使用Apache HttpComponents,您可以在不使用浏览器的情况下导航,您看了吗?