我正在尝试使用Dropbox核心API运行应用程序。
示例程序会将文件上传到我的Dropbox帐户。 我从Dropbox获得了应用密钥和秘密。我试图运行示例程序,但卡住了。在授权应用程序时遇到一些问题。
示例程序将打印一个URL,我们必须复制URL才能授权该应用程序。然后单击“允许”来授权应用程序,我收到此消息:
"Enter this code into agri to finish the process".
关于代码
{
// 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 (you might have to log in first)");
System.out.println("3. Copy the authorization code.");
//got stuck over here
String code = new BufferedReader(new InputStreamReader(System.in)).readLine().trim();
DbxAuthFinish authFinish = webAuth.finish(code);
DbxClient client = new DbxClient(config, authFinish.accessToken);
System.out.println("Linked account: " + client.getAccountInfo().displayName);
File inputFile = new File("serialData.xlsx");
inputStream = new FileInputStream(inputFile);
DbxEntry.File uploadedFile = client.uploadFile("serialData.xlsx",DbxWriteMode.add(), inputFile.length(), inputStream);
System.out.println("Uploaded: " + uploadedFile.toString());
}
答案 0 :(得分:0)
如果您在例如Eclipse中运行示例应用程序,则代码执行将停止并等待您获取授权代码(请参阅readLine()提示)。然后将其粘贴到Eclipse(或任何命令提示符)的控制台中。然后代码将被传递给另一个方法来获取和“访问令牌”这是另一个字符串。这是您需要保存到磁盘以供将来使用的内容。我稍微改变了一下代码,以显示需要保存的字符串标记以供将来的api调用。
//Include the Dropbox SDK.
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 = "myappkey"; // change with yours
final String APP_SECRET = "supersecret"; // change with yours
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();
// DbxAuthFinish authFinish = webAuth.finish(code);
// System.out.println("Access token is:");
// System.out.println(authFinish.accessToken.toString());
// save the value of myToken to a file for future use
String myToken = "mybigsecrettokenxxxxxxxxxxxxxxxxxxxxx"; // change with
// yours
// DbxClient client = new DbxClient(config, authFinish.accessToken);
DbxClient client = new DbxClient(config, myToken);
System.out.println("Linked account: "
+ client.getAccountInfo().displayName);
System.out.println("check1");
File inputFile = new File("C:\\dev\\pi\\foo.txt");
System.out.println("check2");
FileInputStream inputStream = new FileInputStream(inputFile);
System.out.println("check3");
try {
DbxEntry.File uploadedFile = client.uploadFile("/main/fooup2.txt",
DbxWriteMode.add(), inputFile.length(), inputStream);
System.out.println("Uploaded: " + uploadedFile.toString());
} catch (Exception e) {
e.printStackTrace();
} finally {
inputStream.close();
}
DbxEntry.WithChildren listing = client.getMetadataWithChildren("/main");
System.out.println("Files in the root path:");
for (DbxEntry child : listing.children) {
System.out.println(" " + child.name + ": " + child.toString());
}
// download file
FileOutputStream outputStream = new FileOutputStream(
"C:\\dev\\pi\\downloadedfile.txt");
try {
DbxEntry.File downloadedFile = client.getFile("/main/fooup2.txt",
null, outputStream);
System.out.println("Metadata: " + downloadedFile.toString());
} finally {
outputStream.close();
}
}
}