Home > Software engineering >  upload file to dropbox by java
upload file to dropbox by java

Time:11-10

I am working on creating a simple desktop program in Java, and I want to upload files via this program to Dropbox, but the problem is that the access token has a short life (temporary), how can I make the access token have a long life, or if I can use the App key and App secret?

I need a simple solution like a method or a java example.

Is there anything better than Dropbox in this aspect and more flexible?

Thanks for any help.

This method works fine but the access token expires after a few hours

    private void testUplaod() throws FileNotFoundException, IOException, DbxException {
        DbxClientV2 client;
        DbxRequestConfig config = new DbxRequestConfig("dropbox/TestUplaod");
        
        try (InputStream in = new FileInputStream("D:\\t1.txt")) {
            client = new DbxClientV2(config, ACCESS_TOKEN);

            FileMetadata metadata = client.files().uploadBuilder("/t1.txt")
                    .uploadAndFinish(in);
        }

I was expecting it would work sustainably.

CodePudding user response:

When you get the access token, you should also receive a refresh token. When the access token expires, you make an API call with the refresh token to get a new one.

CodePudding user response:

Dropbox is no longer offering the option for creating new long-lived access tokens. Dropbox is switching to only issuing short-lived access tokens (and optional refresh tokens) instead of long-lived access tokens. You can find more information on this migration here.

Apps can still get long-term access by requesting "offline" access though, in which case the app receives a "refresh token" that can be used to retrieve new short-lived access tokens as needed, without further manual user intervention. It's not possible to get a refresh token from the "Generate" button; you need to use the OAuth flow. You can find more information in the OAuth Guide and authorization documentation. There's a basic outline of processing this flow in this blog post which may serve as a useful example.

The official Dropbox Java SDK can actually handle the process for you automatically, as long as you supply the necessary credentials, e.g., as shown retrieved in the examples.

  • Related