我正在为Dropbox开发一个连接器。我必须手动将URL输入到由程序生成的浏览器中,并且单击允许按钮也是手动的。请帮我弄清楚如何自动执行此操作;不应该打开浏览器,只需要一次用户交互。
这是我的代码:
import com.dropbox.core.*;
import java.io.*;
import java.util.Locale;
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 = "; //redacted
final String APP_SECRET = ""; //redacted
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.");
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);
String accessToken = authFinish.accessToken;
DbxClient client = new DbxClient(config, accessToken);
System.out.println("Linked account: " + client.getAccountInfo().displayName);
File inputFile = new File("working-draft.txt");
FileInputStream inputStream = new FileInputStream(inputFile);
try {
DbxEntry.File uploadedFile = client.uploadFile("/magnum-opus.txt",
DbxWriteMode.add(), inputFile.length(), inputStream);
System.out.println("Uploaded: " + uploadedFile.toString());
} finally {
inputStream.close();
}
DbxEntry.WithChildren listing = client.getMetadataWithChildren("/");
System.out.println("Files in the root path:");
for (DbxEntry child : listing.children) {
System.out.println(" " + child.name + ": " + child.toString());
}
FileOutputStream outputStream = new FileOutputStream("magnum-opus.txt");
try {
DbxEntry.File downloadedFile = client.getFile("/magnum-opus.txt", null,
outputStream);
System.out.println("Metadata: " + downloadedFile.toString());
} finally {
outputStream.close();
}
}
}
答案 0 :(得分:0)
用户授权应用的唯一方法是通过浏览器中的OAuth进行授权。因此,在用户首次授权您的应用时,您将无法避免打开浏览器。 (在初始身份验证之后,您可以存储和重用访问令牌,但是第一次运行应用程序时,他们必须在浏览器中登录Dropbox。)
答案 1 :(得分:0)
OAuth需要两个步骤。 您必须先使用DropBox注册您的应用程序。 Box将为您提供一个字符串,您应该在用户授权期间将其包含在您的通话中。
如果用户授权您的应用程序使用他们的资源,那么他们会给您一个令牌,您将来必须使用该令牌。
了解OAuth的工作原理:https://www.pingidentity.com/resource-center/oauth-essentials.cfm
和
https://www.dropbox.com/developers/blog/45/using-oauth-20-with-the-core-api