c# - 如何在 C# 中通过 DropBoxRest API 获取 Dropbox 文件?

标签 c# .net web-services rest dropbox-api

我的目标是通过 C# 中的 DropBoxRest API 获取文件

当我想要获取 token 时,出现以下错误

An unhandled exception of type 'System.AggregateException' occurred in mscorlib.dll

我做错了什么?

这是我的代码

    var options = new Options
{
ClientId = "", //App key
ClientSecret = "", //App secret
RedirectUri = "https://www.dropbox.com/1/oauth2/authorize"
};
        // Initialize a new Client (without an AccessToken)
        var client = new Client(options);

        // Get the OAuth Request Url
        var authRequestUrl = await client.Core.OAuth2.AuthorizeAsync("");

        // TODO: Navigate to authRequestUrl using the browser, and retrieve the Authorization Code from the response
        var authCode = ""; Which code have to be here ???

        // Exchange the Authorization Code with Access/Refresh tokens
        var token = await client.Core.OAuth2.TokenAsync(authCode); in this line occured the following error
          //An unhandled exception of type 'System.AggregateException' occurred in mscorlib.dll      

        // Get account info
        var accountInfo = await client.Core.Accounts.AccountInfoAsync();

最佳答案

您似乎正在使用my library ,所以我会尽力回复。

Dropbox API 使用 OAuth 2.0 流程,这意味着您需要将用户重定向到 Dropbox 页面进行身份验证,并批准您的应用访问其数据。

调用 AuthorizeAsync 后(注意 "code" 参数):

var authRequestUrl = await client.Core.OAuth2.AuthorizeAsync("code");

您将收到一个可将用户重定向到的 URL。该 URL 将在 Dropbox 中打开一个页面以进行身份​​验证和批准。之后,用户将被重定向回您在选项中的 RedirectUri 中提供的 URL。重定向将在 URL 的查询字符串中包含代码。

这意味着您应该有一些服务器来监听您的 RedirectUri。例如:

var options = new Options
    {
        ClientId = "", //App key
        ClientSecret = "", //App secret
        RedirectUri = "https://www.myserver.com/Dropbox/SetCode"
    };

如果您使用 MVC, Controller 中可能有一个操作,如下所示:

public class DropboxController : Controller
{
    public ActionResult SetCode(string code, string error) {}
}

检索到代码后,您可以调用TokenAsync():

var token = await client.Core.OAuth2.TokenAsync(authCode);

您的代码基于我的库中的示例,它希望您手动复制 authRequestUrl,在浏览器中打开它,然后手动检索代码。

注意:有几种方法可以在没有服务器的情况下工作,但这些方法超出了库本身的范围。如果有足够的需求,我可能会考虑将它们包括在内。

关于c# - 如何在 C# 中通过 DropBoxRest API 获取 Dropbox 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28833004/

相关文章:

c# - 为什么C#编译符号不建议缩进?

c# - 在运行时清除 asp.net 窗体

c# - Protobuf-Net 的实现可以击败我目前拥有的吗?

c# - 如何编码为同一原始字符串字符提供不同编码字符的字符串?

.net - Sitecore 自定义后端应用

django - 当从 iPad 接收 JSON Ajax 请求时,如何使用 Django 表单?

作为 ASMX 公开的 WCF 服务不接受参数

c# - Quartz.Net CronExpression 生成器

.net - WPF ListView 非事件选择颜色和元素字体颜色

web-services - 是否有在线用户代理数据库?