gpt4 book ai didi

java - 如何将 dropbox 的 auth token 直接获取到 java 程序中,这样用户就不需要复制粘贴了

转载 作者:行者123 更新时间:2023-11-29 03:26:30 26 4
gpt4 key购买 nike

我正在开发一个应用程序,我必须从 Dropbox 帐户上传文件和下载文件,该应用程序是一个 Java 桌面应用程序。我已经使用了示例代码并且能够毫无故障地运行它。但我想绕过用户需要将授权代码表单浏览器复制到应用程序,该怎么做。我希望我的应用程序将授权 token 直接获取到应用程序中,因为这会导致用户开销。请各位高手帮帮我。这是我已实现的代码。

import com.dropbox.core.*;

import java.awt.Desktop;
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 = "xxxxxxxxxxxxxxxx";
final String APP_SECRET = "xxxxxxxxxxxxxxxx";

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.");
Desktop.getDesktop().browse(java.net.URI.create(authorizeUrl));
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);

DbxClient client = new DbxClient(config, authFinish.accessToken);

System.out.println("Linked account: " + client.getAccountInfo().displayName);

File inputFile = new File("OpenCv_Video_display_link.txt");
FileInputStream inputStream = new FileInputStream(inputFile);
try {
DbxEntry.File uploadedFile = client.uploadFile("/OpenCv_Video_display_link.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("121verbs.pdf");
try {
DbxEntry.File downloadedFile = client.getFile("/121verbs.pdf", null,
outputStream);
System.out.println("Metadata: " + downloadedFile.toString());
} finally {
outputStream.close();
}
}
}

最佳答案

  1. 转到 https://www.dropbox.com/developers/apps
  2. 点击应用程序。在 Outh 2 下的此页面上,查找生成的访问 token 。
  3. 点击“生成”。
  4. 这是您的访问 token 。这样可以验证用户就是您,您不必每次都通过标准授权流程。

不需要的代码我已经注释掉了,你只需要最后一行。

/* // 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 = "Your access token goes here";

关于java - 如何将 dropbox 的 auth token 直接获取到 java 程序中,这样用户就不需要复制粘贴了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20744265/

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