【问题标题】:Assistance with Oauth2 authentication to use with DropBox协助使用 DropBox 进行 Oauth2 身份验证
【发布时间】:2014-02-17 06:26:23
【问题描述】:

我正在构建一个需要将文件上传到 DropBox 的 Windows c# 应用程序。基本上我拥有我的应用程序所需的一切(应用程序密钥和应用程序密钥),但我需要将客户端令牌保存到我的 sql DB 以供将来使用。根据 Dropbox 的说法,我无法保存用户登录信息,这很好,但是找到一个好的库变得越来越困难。我尝试了许多不同的基于 DropBox 的库,但遇到了以下问题:

SharpBox:看起来很容易使用,但需要某种反序列化器来将客户端密钥和客户端密码保存在任何地方。

OAuth2 授权方:我找不到足够的文档,无法实际实现。

DropNet:这是一个看起来很有希望的网络。它是异步的,看起来不错,但我再次找不到如何执行 auth 函数并将变量保存到文件/DB/Reg/ 或任何内容的示例。

DropBox.API:这是我目前使用的方法,它正在工作。问题是它不是异步的,需要 .NET 4.5。我对所有的问题都很满意,但最近发现这对不同版本的 JSON 和其他库非常敏感。

我希望有人可以帮助我让上述任何 OAUTH 库真正工作,只是为了让 3 腿身份验证过程正常工作。

更新::

好的,所以我将包含一些我目前正在使用的代码,这些代码使用 dropbox.api:

// Get Oauth Token
    private static OAuthToken GetAccessToken()
    {
        string consumerKey = "mykey";
        string consumerSecret = "myseceret";
        var oauth = new OAuth();
        var requestToken = oauth.GetRequestToken(new Uri(DropboxRestApi.BaseUri), consumerKey, consumerSecret);
        var authorizeUri = oauth.GetAuthorizeUri(new Uri(DropboxRestApi.AuthorizeBaseUri), requestToken);
        Process.Start(authorizeUri.AbsoluteUri);
        MessageBox.Show("Once Registration is completed Click OK", "Confirmation");
        return oauth.GetAccessToken(new Uri(DropboxRestApi.BaseUri), consumerKey, consumerSecret, requestToken);
    }

        // Complete Oauth function and write to file
    private void button5_Click(object sender, EventArgs e)
    {
        DialogResult result1 = MessageBox.Show("Please register for dropbox before continuing with authentication. The authorization process will take 1 minute to complete. During that time the backup utility window will be unresponsive. Click yes if you are ready to begin the authorization. HAVE YOU REGISTERED FOR DROPBOX YET?", "DO YOU HAVE A DROPBOX ACCOUNT?", MessageBoxButtons.YesNo);
        if (result1 == DialogResult.Yes)
        {
            try
            {
                u_w.Enabled = false;
                var accesstoken = GetAccessToken();
                StringBuilder newFile = new StringBuilder();
                string temptoken = "";
                string tempsecret = "";
                string tempprovider = "";
                string tempstatus = "";
                string[] file = System.IO.File.ReadAllLines(@"C:\cfg\andro_backup.ini");
                foreach (string line in file)
                {
                    if (line.Contains("dbkey:"))
                    {
                        temptoken = line.Replace("dbkey:", "dbkey:" + accesstoken.Token);
                        newFile.Append(temptoken + "\r\n");
                        continue;
                    }
                    if (line.Contains("dbsecret:"))
                    {
                        tempsecret = line.Replace("dbsecret:", "dbsecret:" + accesstoken.Secret);
                        newFile.Append(tempsecret + "\r\n");
                        continue;
                    }
                    if (line.Contains("Provider:"))
                    {
                        tempprovider = line.Replace("Provider:", "Provider:DropBox");
                        newFile.Append(tempprovider + "\r\n");
                        continue;
                    }
                    if (line.Contains("Status:"))
                    {
                        tempstatus = line.Replace("Status:", "Status:Connected");
                        newFile.Append(tempstatus + "\r\n");
                        continue;
                    }
                    newFile.Append(line + "\r\n");
                }
                System.IO.File.WriteAllText(@"C:\cfg\andro_backup.ini", newFile.ToString());
                MessageBox.Show("Completed Backup Provider Setup", "Provider Setup Complete");
                Configuration.Reload();

目前以上工作,我可以上传,下载文件。问题是它不是异步的,如果可能的话,我想尝试留在 .NET 4.0 中,此代码需要 4.5

尝试使用 dropnet 做同样的事情,即使使用他在位于此处 https://github.com/dkarzon/DropNet 的页面上提供的示例,我也无法让它工作。 我也尝试查看他在那里的演示,但他们解释说让用户每次登录以执行任何功能,我需要授权应用程序以便它可以在需要时执行它的行为。至于我用于 drop net 的代码,我实际上只是复制并粘贴了他那里的内容,只是为了看看我是否可以让它连接起来,但还是不行。

【问题讨论】:

  • 您能使用 DropNet 发布您的代码吗?使用保存的访问令牌看起来非常简单,但是如果不看到您尝试过的内容,就很难说出您缺少什么。

标签: c# oauth-2.0 dropbox


【解决方案1】:

如果您使用类似于示例的 DropNet,您需要做的就是保存来自 GetAccessToken 方法的返回对象。这将返回一个 UserLogin 对象的实例,该对象上有 Token 和 secret。或者,如果您使用的是异步方法,则使用回调参数。

在此处查看示例: https://github.com/dkarzon/DropNet/blob/master/DropNet.Samples/DropNet.Samples.WP7/MainPage.xaml.cs#L69

发布您正在使用的代码,以便我可以为您提供更好的解释。

【讨论】:

  • 确实有帮助,我又看了一遍文档,最后把东西拼凑起来。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-29
相关资源
最近更新 更多