我的目标是通过 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/