mat1c
mat1c

Reputation: 95

C# Dropbox Api retrieve files of public shared folder

i would like to ask, if there's any way to retrieve files links of folder which is publicly shared. Like someone create random public folder(everyone can view it) and put some random files into it. So i need to get all files links from that folder. All i know is link to that folder in format: https://www.dropbox.com/sh/[code]/[code]. Can i do that by using dropbox api, or the only option is to scrape dropbox page directly?

Upvotes: 1

Views: 3502

Answers (2)

Artur Kedzior
Artur Kedzior

Reputation: 4263

Here is a copy paste example:

using Dropbox.Api;
using Dropbox.Api.Files;
...
// AccessToken - get it from app console 
// FolderToDownload - https://www.dropbox.com/sh/{unicorn_string}?dl=0

using (var dbx = new DropboxClient(_dropboxSettings.AccessToken))
{
    var sharedLink = new SharedLink(_dropboxSettings.FolderToDownload);
    var sharedFiles = await dbx.Files.ListFolderAsync(path: "", sharedLink: sharedLink);

    foreach (var file in sharedFiles.Entries)
    {

    }        
}

The documentation wasn't clear about setting path to an empty string when using it with publicly shared folders.

Upvotes: 2

Greg
Greg

Reputation: 16930

The official way to get information about a particular shared link is to use the Dropbox API's /2/sharing/get_shared_link_metadata endpoint:

https://www.dropbox.com/developers/documentation/http/documentation#sharing-get_shared_link_metadata

In the official Dropbox .NET SDK that's the GetSharedLinkMetadataAsync method:

https://dropbox.github.io/dropbox-sdk-dotnet/html/M_Dropbox_Api_Sharing_Routes_SharingUserRoutes_GetSharedLinkMetadataAsync_1.htm

This unfortunately doesn't offer the list of files though. We'll consider that a feature request.

Note that scraping the site would be error prone and likely to break without warning. (It's also against the terms anyway.)


Edit:

Dropbox API v2 now supports listing the contents of a shared link for a folder. This can be accomplished using the same interface as listing a folder in a connected user's account, via the list_folder functionality. To list the contents of a shared link for a folder, you instead provide the shared link URL in the shared_link parameter to /2/files/list_folder:

https://www.dropbox.com/developers/documentation/http/documentation#files-list_folder

If you're using an official SDK, there will also be a corresponding method for this endpoint. In the .NET SDK that's available as ListFolderAsync:

https://dropbox.github.io/dropbox-sdk-dotnet/html/M_Dropbox_Api_Files_Routes_FilesUserRoutes_ListFolderAsync_1.htm

Upvotes: 1

Related Questions