65.9K
CodeProject 正在变化。 阅读更多。
Home

用于 DropBox 云服务的基于 RestSharp 的简单单页控制台应用程序

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.60/5 (3投票s)

2013年3月28日

LGPL3

2分钟阅读

viewsIcon

43608

downloadIcon

927

本文介绍了如何使用 RestSharp SDK 为 DropBox 云服务构建应用程序。

引言

本文档适用于希望使用 DropBox 云服务开发桌面应用程序,但面对大量可用资源和现有贡献而犹豫不决的开发人员。 如果您是本文的读者,那么简单易用的“复制粘贴”代码正在等待着您! Smile | :)

背景

如今,云服务是每个人都想使用的东西。 加入流行的云组看起来既简单又快速。 特定云服务提供商的文档页面通常包含大量信息,并且由于云的分布式特性,通常指定自定义的 REST 样式 API。 此类规范足以理解服务提供商提供的许多功能,但并没有让我们更接近使用任何常规开发工具和现代编程语言的实际实现。

RestSharp SDK 提供了许多已实现的功能,例如 OAuth v1 和 OAuth v2 协议、REST 协议等等,甚至还为我们提供了一个 框架,展示了如何在原生样式中使用 RestSharp SDK。

在学习和使用应用程序之前,请确保您已经完成了以下步骤:注册 DropBox 并创建 DropBox 应用程序(即,获取 App key/App secret 对)。 您可以在 DropBox 开发资源 此处 阅读说明这些简单步骤的说明。

使用代码

本文为您提供完整的 MS Visual Studio 2010 Express 应用程序,该应用程序能够获取 OAuth v1 请求令牌、用户授权、访问令牌并检索用户的帐户信息,作为 DropBox 服务使用的常规示例。

让我们逐步了解应用程序中最有趣的部分。

配置应用程序

将您获得的 App key/App secret 对放入下面的常量字符串中。 这是一个重要的步骤,也是唯一需要您贡献以使其真实有效的代码片段

private const string mc_apiKey = "YOUR_API_KEY";
private const string mc_appsecret = "YOUR_APP_SECRET";

获取请求令牌

此步骤对应于 /oauth/request_token 操作

//
// Get request token
//

var baseUrl = "https://api.dropbox.com";

var client = new RestClient(baseUrl);
client.Authenticator = OAuth1Authenticator.ForRequestToken(mc_apiKey, mc_appsecret);
var request = new RestRequest(string.Format("/{0}/oauth/request_token", mc_version), Method.POST);

var response = client.Execute(request);

if (response.StatusCode != HttpStatusCode.OK)
{
    break;
}

OAuthToken requestToken = new OAuthToken();
{
    var qs = HttpUtility.ParseQueryString(response.Content);
    var oauth_token = qs["oauth_token"];
    var oauth_token_secret = qs["oauth_token_secret"];

    requestToken.Token = oauth_token;
    requestToken.Secret = oauth_token_secret;
}

获取用户访问权限的授权

此步骤对应于 /oauth/authorize 操作

//
// Get approval from user for the access
//

// Create a HttpListener for the specified url.
string AuthorizationCallBackURL = string.Format(LoopbackCallback, 
  auth_GetRandomUnusedPort(), Assembly.GetEntryAssembly().GetName().Name);


request = new RestRequest(string.Format("/{0}/oauth/authorize", mc_version));
request.AddParameter("oauth_token", requestToken.Token);
request.AddParameter("oauth_callback", AuthorizationCallBackURL);
var url = client.BuildUri(request).ToString();

bool bUserAcceptedRequest = false;

var resetEvent = new ManualResetEvent(false);
using (var svr = SimpleServer.Create(AuthorizationCallBackURL, context =>
{
    string uid = context.Request.QueryString["uid"];
    string code = context.Request.QueryString["oauth_token"];

    if (!string.IsNullOrEmpty(uid) && !string.IsNullOrEmpty(code))
    {
        bUserAcceptedRequest = true;
    }
    resetEvent.Set();

}))
{
    System.Diagnostics.Process.Start(url);

    resetEvent.WaitOne();

}

if (false == bUserAcceptedRequest)
{
    break;
}

获取访问令牌

此步骤对应于 /oauth/access_token 操作

//
// Get access token
//

var verifier = "123456";
request = new RestRequest(string.Format("/{0}/oauth/access_token", mc_version), Method.POST);
client.Authenticator = OAuth1Authenticator.ForAccessToken(
mc_apiKey, mc_appsecret, requestToken.Token, requestToken.Secret, verifier
);
response = client.Execute(request);

if (response.StatusCode != HttpStatusCode.OK)
{
    break;
}

accessToken = new OAuthToken();
{
    var qs = HttpUtility.ParseQueryString(response.Content);
    var oauth_token = qs["oauth_token"];
    var oauth_token_secret = qs["oauth_token_secret"];

    accessToken.Token = oauth_token;
    accessToken.Secret = oauth_token_secret;
}

获取用户帐户信息

此步骤对应于 /account/info 操作

//
// Below is a sample how to access any DropBox service regular using the valid access token
//

request = new RestRequest(string.Format("/{0}/account/info", mc_version));
client.Authenticator = OAuth1Authenticator.ForProtectedResource(
mc_apiKey, mc_appsecret, accessToken.Token, accessToken.Secret
);

var responseAccount = client.Execute<AccountInfo>(request);

if (responseAccount.StatusCode != HttpStatusCode.OK)
{
    break;
}

AccountInfo acntInfo = responseAccount.Data;
if (null == acntInfo)
{
    break;
}

Console.WriteLine("First step tokens: token:{0} secret:{1}", requestToken.Token, requestToken.Secret);
Console.WriteLine("Second step tokens: token:{0} secret:{1}", accessToken.Token, accessToken.Secret);
Console.WriteLine("Account Info: {0}", responseAccount.Content);

关注点

本文的主要目标是为 C# 开发人员配备完整的参考应用程序,以加快了解其工作原理、从何处开始以及如何使其工作的过程 Smile | :)

让我做一些小小的贡献来帮助其他正在寻找这种帮助的开发人员。

谢谢

我想说一些好话来感谢作为本文背景的贡献

历史

  • 2013-03-28:初始版本。
© . All rights reserved.