We are aware of the issue with the badge emails resending to everyone, we apologise for the inconvenience - learn more here.

Forum Discussion

TC888's avatar
TC888
Explorer | Level 3
3 years ago

Downloading files in a shared folder

I have an URL for a folder with files that I need to download using the API. Although the folder is public, and I'm able to download files from it on the website without any kind of authorizaiton or credentials, I'm not sure how appropriate it is to post it in a public forum. As an example that's similar, and gets me the same results, I'll use this URL:
https://www.dropbox.com/sh/p0fa8tl8zzmetuv/AABKUyvlxZGIOvxPWPoothbra?dl=0
for the folder, and this one:
https://www.dropbox.com/sh/p0fa8tl8zzmetuv/AABKUyvlxZGIOvxPWPoothbra/bar/baz.txt?dl=0
for a file.

(I found the example in this post:
https://www.dropboxforum.com/t5/Discuss-Dropbox-Developer-API/API-method-to-download-file-within-shared-folder-no-token/m-p/427727
Hopefully, the author of that post won't mind my using it here.)

 

With the Java SDK, if I try to use client.sharing().getSharedLinkMetadata(fileUrl) on the folder URL it works, but on the file URL I get SHARED_LINK_ACCESS_DENIED. I guess the file URL isn't technically a shared link, but it's in a shared folder, so there should be some way to get it. If I try to download the file with client.files().download(path) using the folder ID from the metadata in the path (id:kLMXpr_BUfIAAAAAAAQJqg/bar/baz.txt), I get not_found. If I construct the path from the URL (/sh/p0fa8tl8zzmetuv/AABKUyvlxZGIOvxPWPoothbra/bar/baz.txt) I also get not_found. I tried mounting the folder using client.sharing().mountFolder(folderId), but that got invalid_id.

I must be missing something. Any suggestions?

 

Also, why do my access tokens keep expiring? I thought they were supposed to be more or less permanent.

  • To interact with files in a folder via a shared link to that folder, you shouldn't modify the shared link. You should instead use the API options to specify the relative path of the file inside the folder. Here's some example code showing the different things you can do:

    // this shared link is for a folder
    String sharedLinkUrl = "https://www.dropbox.com/sh/sm8nzbmxo1khsb7/AAAYre26ySxEvrnLzvKMYOvma?dl=0";
    
    // to get the metadata for a shared link:
    System.out.println(client.sharing().getSharedLinkMetadata(sharedLinkUrl));
    
    // in this case, there's a file named "test.txt" in the linked folder:
    String relativeFilePath = "/test.txt";
    // to get the metadata for a file in a folder from a shared link for the folder:
    System.out.println(client.sharing().getSharedLinkMetadataBuilder(sharedLinkUrl).withPath(relativeFilePath).start());
    
    // or to programmatically list the contents of a folder from a shared link for the folder:
    System.out.println(client.files().listFolderBuilder("").withSharedLink(new SharedLink(sharedLinkUrl)).start());
    // be sure to implement listFolderContinue too
    
    // to download a file in a folder from a shared link for the folder:
    System.out.println(client.sharing().getSharedLinkFileBuilder(sharedLinkUrl).withPath(relativeFilePath).start());
  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Staff rankDropbox Staff

    To interact with files in a folder via a shared link to that folder, you shouldn't modify the shared link. You should instead use the API options to specify the relative path of the file inside the folder. Here's some example code showing the different things you can do:

    // this shared link is for a folder
    String sharedLinkUrl = "https://www.dropbox.com/sh/sm8nzbmxo1khsb7/AAAYre26ySxEvrnLzvKMYOvma?dl=0";
    
    // to get the metadata for a shared link:
    System.out.println(client.sharing().getSharedLinkMetadata(sharedLinkUrl));
    
    // in this case, there's a file named "test.txt" in the linked folder:
    String relativeFilePath = "/test.txt";
    // to get the metadata for a file in a folder from a shared link for the folder:
    System.out.println(client.sharing().getSharedLinkMetadataBuilder(sharedLinkUrl).withPath(relativeFilePath).start());
    
    // or to programmatically list the contents of a folder from a shared link for the folder:
    System.out.println(client.files().listFolderBuilder("").withSharedLink(new SharedLink(sharedLinkUrl)).start());
    // be sure to implement listFolderContinue too
    
    // to download a file in a folder from a shared link for the folder:
    System.out.println(client.sharing().getSharedLinkFileBuilder(sharedLinkUrl).withPath(relativeFilePath).start());
  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Staff rankDropbox Staff

    As for the access tokens, note that Dropbox is in the process of 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. You can find more information in the OAuth Guide and authorization documentation.

     

    You can find examples of handling the authorization flow with the Dropbox Java SDK in particular here.

    For reference, while the creation of new long-lived access tokens is now deprecated, we don't currently have a plan to disable existing long-lived access tokens. (If that changes, we will of course announce that ahead of time.) That being the case, you can continue using existing long-lived access token(s) without interruption, if you have any. Also, note though that after the change you won't be able to create new long-lived access tokens.

    While the change began on September 30th 2021, we released it gradually, so you may not have seen your app(s) affected until more recently. Once it applies to your app, it would apply regardless of the "Access token expiration" setting for your app, and that setting may no longer be available for your app.

    • TC888's avatar
      TC888
      Explorer | Level 3

      Thanks for the help.  That was exactly the missing piece I needed.

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

5,876 PostsLatest Activity: 40 minutes ago
325 Following

If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X or Facebook.

For more info on available support options for your Dropbox plan, see this article.

If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!