将 ASP.NET Web 应用程序连接到 Dropbox

发布于 2025-01-08 01:08:47 字数 288 浏览 0 评论 0原文

我正在开发一个网络应用程序,我想让我的用户能够连接个人 Dropbox 帐户。 我将构建文件和文件夹浏览器,但我希望能够首先访问它们。

我有来自 Dropbox 的 ApiKey 和 ApiSecret 用于我的应用程序。

我尝试使用 C# 框架,但我认为这些框架适用于桌面/winform/控制台/移动应用程序。

我能够获得 request_token 但无法获得 access_token。

有人可以解释一下这个问题吗? (使用 Dropbox API 和 ASP.NET Web 应用程序)。

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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

面犯桃花 2025-01-15 01:08:47

Sharpbox 好像已经有一段时间没有更新了。您可能想要结账

https://github.com/dkarzon/DropNet

它位于 nuget 上,只需在 packagemanager 控制台中使用它:

安装包 DropNet

在此博客上了解更多相关信息:
http://dkdevelopment.net/what-im-doing/dropnet/

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/

_蜘蛛 2025-01-15 01:08:47

我找到了完美的解决方案。
我使用了 SharpBox .NET 库...

我使用了它,我能够读取文件夹、获取文件、读取文件内容以供下载等等!

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!

╰沐子 2025-01-15 01:08:47

我不知道如何使用 Dropbox,但我的应用程序正在使用 www.DriveHQ.com,工作完美,DriveHQ 为我提供了一个私人网站,这太棒了。

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.

泡沫很甜 2025-01-15 01:08:47

检查 Spring.NET Social Dropbox:http://www.springframework.net/social-dropbox/
发行版中有一个完整的 ASP.NET 示例。

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

小嗷兮 2025-01-15 01:08:47

下面是一个示例(使用 ASP MVC4 和 SharpBox .NET),展示了一个很好的演示。

首先,它尝试从 App_Data 中的文件加载访问令牌。如果存在,请使用令牌读取 Dropbox 上的应用程序文件夹。

如果不存在,请查看当前会话中是否有请求令牌。如果可用,用户可能已向 Web 应用程序授予访问其保管箱的权限,因此请尝试将请求令牌转换为访问令牌。

如果没有请求令牌,请创建它,将其存储在会话中并将用户重定向到 Dropbox,以便系统提示他授予 Web 应用程序对其文件夹的访问权限。

注意:该代码是一个演示。要将其变成多用户和真实场景,您将需要存储每个用户的访问令牌文件,并且应该将访问令牌缓存在会话缓存中,以防止对访问令牌文件的多余读取。

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;
    }
}

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文