gpt4 book ai didi

java - Dropbox Core API JAVA 授权代码

转载 作者:行者123 更新时间:2023-12-02 05:05:21 25 4
gpt4 key购买 nike

使用保管箱 core api tutorial我可以上传文件。

但是,我的问题是这个 SO post 的精确复制品--- 也就是说,一旦我有了授权代码并注释掉了用户身份验证行,这样我就不必每次使用 dropbox 时都手动重新授权批准,我会收到以下错误:

Exception in thread "main" com.dropbox.core.DbxException$BadRequest: {"error_description": "code has already been used", "error": "invalid_grant"}

或者

Exception in thread "main" com.dropbox.core.DbxException$BadRequest: {"error_description": "code has expired (within the last hour)", "error": "invalid_grant"}

我确信我拥有正确的授权代码。

我希望我遗漏了一些东西,否则如果每次使用 API 时都必须进行手动干预,那么它还有什么意义呢?

编辑:我的确切代码( key 已被打乱)

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();
}
}
}

最佳答案

您应该存储和重用访问 token ,而不是授权代码。

执行此操作一次后:

String accessToken = authFinish.accessToken;

你应该将整个内容替换为

String accessToken = "<the one you already got>";

顺便说一句,如果您只需要为自己的帐户提供访问 token ,只需单击一个按钮即可生成一个!请参阅https://www.dropbox.com/developers/blog/94/generate-an-access-token-for-your-own-account .

关于java - Dropbox Core API JAVA 授权代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27853375/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com