Question

I'm working on a web application and i want to give my users the ability to connect a personal Dropbox account. I will build the files and folders browser but i want to be able to access them first.

I have a ApiKey and ApiSecret from Dropbox for my application.

I tried to use the C# frameworks but i think those are for desktop/winform/console/mobile applications.

I was able to get a request_token but i cant get access_token.

Can someone please put some light on this issue? (working with Dropbox API with ASP.NET web application).

Was it helpful?

Solution 2

I found the perfect solution. I used the SharpBox .NET library...

I played with it and i was able to Read folders, get files, read file content for download and more!

OTHER TIPS

It seems that Sharpbox hasn't been updated for a while. You might want to checkout

https://github.com/dkarzon/DropNet

it is on nuget, just use this in the packagemanager console:

Install-Package DropNet

read more about it on this blog: http://dkdevelopment.net/what-im-doing/dropnet/

I am not sure how to work with Dropbox, but my application is working with www.DriveHQ.com, work perfectly, DriveHQ provides me a private website, that's amazing.

Check Spring.NET Social Dropbox: http://www.springframework.net/social-dropbox/ There is a full ASP.NET example in the distribution.

Here is a sample (using ASP MVC4 and SharpBox .NET) that shows a nice demo.

First it tries to load the access token from a file in App_Data. If it is there, use the token to read the app folder on dropbox.

If it is not there see if there is a request token in the current session. If it is available the user might have granted permission to the web application to access his dropbox so try to turn the request token into an access token.

If there is no request token, create it, store it in the session and redirect the user to dropbox so he will be prompted to give the web application access to his folder.

Note: the code is a demo. To turn it into a multi user and real scenario you will need to store the access token file per user and you should cache the access token in the session cache to prevent superfluous reading of the access token file.

public class HomeController : Controller
{
    public ActionResult Index()
    {
        string tokenFile = Path.Combine (Server.MapPath("~/App_Data"), "AccessTokens/dropbox.xml");
        string appKey = "<<appkey>>";
        string appSecret = "<<appsecret>>";
        ICloudStorageAccessToken accessToken;
        CloudStorage storage = new CloudStorage();

        DropBoxConfiguration config = 
            CloudStorage.GetCloudConfigurationEasy(nSupportedCloudConfigurations.DropBox) as DropBoxConfiguration;

        if (TryLoadAccessToken(tokenFile, storage, out accessToken))
        {
            storage.Open(config, accessToken);
            var appFolder = storage.GetRoot();

            var folderContent = new List<Tuple<string, bool>>();
            foreach (var entry in appFolder) 
            { 
                bool isFolder = entry is ICloudDirectoryEntry; 
                folderContent.Add(new Tuple<string, bool>(entry.Name, isFolder));
            }
            ViewBag.FolderContent = folderContent;
        }
        else
        {
            ICloudStorageAccessToken requestToken;
            if (TryLoadRequestToken(out requestToken))
            {
                if (requestToken is DropBoxRequestToken)
                {
                    accessToken = 
                        DropBoxStorageProviderTools.ExchangeDropBoxRequestTokenIntoAccessToken(
                            config, appKey, appSecret, (DropBoxRequestToken)requestToken);

                    storage.Open(config, accessToken);

                    using (FileStream fs = System.IO.File.Create(tokenFile))
                    {
                        storage.SerializeSecurityTokenToStream(accessToken, fs); ;
                    }
                }
                else
                {
                    throw new Exception("The request token is not from Dropbox.");
                }
            }
            else
            {
                config.AuthorizationCallBack = new Uri("http://localhost:57326/");

                requestToken = DropBoxStorageProviderTools.GetDropBoxRequestToken(config, appKey, appSecret);

                Session["dropboxRequestToken"] = requestToken;

                String url = DropBoxStorageProviderTools.GetDropBoxAuthorizationUrl(
                    config, (DropBoxRequestToken)requestToken);

                return new RedirectResult(url);
            }
        }

        return View();
    }

    private bool TryLoadRequestToken(out ICloudStorageAccessToken requestToken)
    {
        requestToken = Session["dropboxRequestToken"] as ICloudStorageAccessToken;
        return requestToken != null;
    }

    private bool TryLoadAccessToken(string tokenFile, CloudStorage storage, out  ICloudStorageAccessToken accessToken)
    {
        accessToken = null;
        try
        {
            using (FileStream fileStream =
                    System.IO.File.Open(tokenFile, FileMode.Open, FileAccess.Read, FileShare.None))
            {
                accessToken = storage.DeserializeSecurityToken(fileStream);
            }
        }
        catch 
        {

        }

        return accessToken != null;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top