Dropbox Core API JAVA授权代码 [英] Dropbox Core API JAVA Authorization Code
问题描述
使用保管箱核心api教程我能够上传文件。
Using the dropbox core api tutorial I am able to upload a file.
但是,我的问题是此 ---也就是说,一旦我有了授权码并注释掉了用户身份验证行,就不必手动进行操作了。每次我使用保管箱时,重新授权都会出现以下错误:
However, my question is an exact replica of this SO post--- That is, once I have my authorization code and comment out the user auth lines so that I dont have to manually re-authorize approval every time I use dropbox I get the following errors:
Exception in thread "main" com.dropbox.core.DbxException$BadRequest: {"error_description": "code has already been used", "error": "invalid_grant"}
OR
Exception in thread "main" com.dropbox.core.DbxException$BadRequest: {"error_description": "code has expired (within the last hour)", "error": "invalid_grant"}
我很肯定我有正确的授权代码。
I am positive I have the correct authorization code.
我希望我遗漏了一些东西,否则,如果每次使用API都要进行人工干预,API的意义是什么?
I hope that I'm missing something, else whats the point of an API if you have to induce manual intervention every time you use it?
编辑:我的确切代码(密钥已被加密)
My Exact Code (keys have been scrambled)
import com.dropbox.core.*;
import java.io.*;
import java.util.Locale;
public class DropboxUpload {
public static void main(String[] args) throws IOException, DbxException {
// Get your app key and secret from the Dropbox developers website.
final String APP_KEY = "2po9b49whx74h67";
final String APP_SECRET = "m98f734hnr92kmh";
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("VtwxzitUoI8DDDLx0PlLut5Gjpw3");
String accessToken = authFinish.accessToken;
DbxClient client = new DbxClient(config, accessToken);
System.out.println("Linked account: " + client.getAccountInfo().displayName);
File inputFile = new File("/home/dropboxuser/Documents/test.txt");
FileInputStream inputStream = new FileInputStream(inputFile);
try {
DbxEntry.File uploadedFile = client.uploadFile("/Public/test.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("test.txt");
try {
DbxEntry.File downloadedFile = client.getFile("/Public/test.txt", null,
outputStream);
System.out.println("Metadata: " + downloadedFile.toString());
} finally {
outputStream.close();
}
}
}
推荐答案
您应该存储和重用访问令牌,而不是授权码。
You should be storing and reusing the access token, not the authorization code.
因此,执行一次之后:
字符串accessToken = authFinish.accessToken;
您应该将整个内容替换为
You should just replace the whole thing with
String accessToken =<您已经拥有的那个>;
顺便说一句,如果您只需要自己的帐户的访问令牌,则可以通过单击一个按钮来生成一个令牌!请参阅 https:// www。 dropbox.com/developers/blog/94/generate-an-access-token-for-您自己的帐户。
BTW, if you just need an access token for your own account, you can generate one with the click of a button! See https://www.dropbox.com/developers/blog/94/generate-an-access-token-for-your-own-account.
这篇关于Dropbox Core API JAVA授权代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!