gpt4 book ai didi

java - 卡在文件上传中,使用 java 在 DropBox 中下载应用程序

转载 作者:太空宇宙 更新时间:2023-11-04 15:07:13 25 4
gpt4 key购买 nike

我正在使用 java 开发 Dropbox api。第一个应用程序是在 Dropbox 帐户中上传和下载文件。我有一个 token 可以使用 dropbox 进行身份验证,但是当我尝试在帐户中上传文件时,我收到错误的请求错误,例如:

Exception in thread "main" com.dropbox.core.DbxException$BadResponse: unexpected response code: 401
at com.dropbox.core.DbxClient$4.handle(DbxClient.java:274)
at com.dropbox.core.DbxClient$4.handle(DbxClient.java:270)
at com.dropbox.core.DbxRequestUtil.doGet(DbxRequestUtil.java:265)
at com.dropbox.core.DbxClient.doGet(DbxClient.java:1912)
at com.dropbox.core.DbxClient.getAccountInfo(DbxClient.java:270)
at com.prit.net.Main.main(Main.java:50)

我的代码位于package com.prit.net下面;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Locale;
import com.dropbox.core.DbxAppInfo;
import com.dropbox.core.DbxClient;
import com.dropbox.core.DbxEntry;
import com.dropbox.core.DbxException;
import com.dropbox.core.DbxRequestConfig;
import com.dropbox.core.DbxWebAuthNoRedirect;
import com.dropbox.core.DbxWriteMode;
public class Main {

public static void main(String[] args) throws IOException, DbxException, URISyntaxException {
// Get your app key and secret from the Dropbox developers website.
final String APP_KEY = "mykey"; // change with yours
final String APP_SECRET = "mysecret"; // 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();
//Desktop.getDesktop().browse(new URL(authorizeUrl).toURI());
// 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 = "myTokensecretkeyxxxxxxxxxxxxxxxxx"; // change with
// yours
// DbxClient client = new DbxClient(config, authFinish.accessToken);
DbxClient client = new DbxClient(config, myToken);
System.out.println(config);
System.out.println(myToken);

System.out.println("check1");
File inputFile = new File("C:\\Dev\\foo.txt");
System.out.println("check2");
FileInputStream inputStream = new FileInputStream(inputFile);
System.out.println("check3");
try {
DbxEntry.File uploadedFile = client.uploadFile("/FileApiDemo/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("/FileApiDemo");
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\\downloadedfile.txt");
try {
DbxEntry.File downloadedFile = client.getFile("/FileApiDemo/fooup2.txt", null, outputStream);
System.out.println("Metadata: " + downloadedFile.toString());
} finally {
outputStream.close();
}

}

} `

最佳答案

仔细检查myToken的值。您上面的注释代码可能会为您提供一个有效的访问 token ,对吧?如果是这样,请确保您在该流程结束时看到的访问 token 是您要保存并在后续运行中使用的访问 token 。

关于java - 卡在文件上传中,使用 java 在 DropBox 中下载应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21831281/

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