【问题标题】:How to get Dropbox files by DropBoxRest API in C#?如何在 C# 中通过 DropBoxRest API 获取 Dropbox 文件?
【发布时间】:2015-03-03 13:26:27
【问题描述】:

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

我想获取令牌时遇到以下错误

“System.AggregateException”类型的未处理异常发生在 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();

【问题讨论】:

  • 在什么时候你在上面的代码中遇到了错误?
  • 此时 var token = await client.Core.OAuth2.TokenAsync(authCode);
  • 你是如何获得 authCode 的?
  • 我不知道如何获得 authCode :) 我认为这是错误的原因
  • 您的代码显示您出于某种原因在 Dropbox 服务器上发出请求。您应该阅读他们的 API 以了解如何获取正确的 authCode。

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


【解决方案1】:

您似乎在使用my library,所以我会尝试回复。

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

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

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

您将收到一个 URL,您可以将用户重定向到该 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 中有一个 Action,如下所示:

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

一旦你检索到code,你就可以调用TokenAsync()

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

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

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

【讨论】:

  • 我属于需要在没有服务器的情况下进行身份验证的人之一(对于 Windows 应用商店应用程序),所以这是我对增强功能的投票!
  • @TyJacobs 你可以从我的 OneDriveRestAPI github.com/saguiitay/OneDriveRestAPI看到一个示例(使用 WPF,但我相信你能理解它)
【解决方案2】:

首先,当您捕获异常时,您可以获得有关此错误的更多详细信息:

try
{
    var token = await client.Core.OAuth2.TokenAsync(authCode);
}
catch (AggregateException aex)
{
    // set a breakpoint on the opening curly brace and check the
    // variable "aex".
}

第二件事是,您需要组合秘密来执行 OAuth。正如here 提到的,你需要一些东西来识别你自己。您的应用针对 OAuth 提供者。为此,您需要提供某种由 OAuth 提供者提供给您的密钥。

在 Dropbox,您需要通过 console 配置所有内容。

编辑

您必须在 DropBox AppConsole 生成一个访问令牌,您可以将其直接传递给客户端选项:

var options = new Options
{
    ClientId = "{see console}", //App key
    ClientSecret = "{see console}", //App secret
    AccessToken = "{see console}",
    RedirectUri = "{see console}"
};
var client = new Client(options);

在这种情况下,您不需要默认获取令牌。

由于投反对票,我将其调试下来。我可以得到以下异常详细信息:

DropboxRestAPI.Models.Exceptions.ServiceErrorException 未处理 消息:在 mscorlib.dll 中发生“DropboxRestAPI.Models.Exceptions.ServiceErrorException”类型的未处理异常 附加信息:invalid_grant

JavaScript-documentation of DropBox 指出这可能表示 API 错误。

【讨论】:

  • 通过你的代码我看不到错误信息,因为错误没有进入 catch 语句
  • 他没有正确传递 authCode,因此出错。还有什么需要知道的?
  • 我的问题是。我做错了什么以及如何获得 authCode
  • 您应该阅读 dropbox api 文档以获取 authCode
  • 怎么样 var authRequestUrl = await client.Core.OAuth2.AuthorizeAsync(""); ?我需要在 AuthorizeAsync 中传递什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-03-05
  • 2016-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多