DropNet无法为后续请求保存访问令牌?

时间:2015-04-08 16:37:16

标签: c# dropbox-api dropnet

在我之前的许多人中,我很难在应用程序的启动之间成功保存访问令牌。这个应用程序实际上只会与一个特定的Dropbox帐户一起使用,不过我试图让它易于设置并且更具动态性。

当然,如果设置为空(初始运行),则用户可以正确登录并授权应用程序,然后返回应用程序按预期工作。然而,在后续运行中,当它从设置集合中获取令牌和秘密时,它会以失败的方式失败

  

收到回复[未经授权]:预计会看到[确定]。 HTTP   回复是[{“错误”:“无效签名。”}]。

我显然做错了什么,这是什么?谢谢!

以下代码!

using System;
using DropNet;

namespace DS_Uploader_DropBox {
    class Program {
        private const string AppKey = "my super secret app key";
        private const string AppSecret = "my super secret app secret";

        static void Main(string[] args) {
            DropNetClient client;
            DropNet.Models.UserLogin token;

            string userToken = Settings.Default.userToken;
            string userSecret = Settings.Default.userSecret;

            bool needAccessToken = (String.IsNullOrEmpty(userToken) || string.IsNullOrEmpty(userSecret));

            //needAccessToken = true;

            if (needAccessToken) {
                client = new DropNet.DropNetClient(AppKey, AppSecret);
                client.UseSandbox = true;
                client.GetToken();

                // Auth with dropbox
                var url = client.BuildAuthorizeUrl();

                // Prompt for user to auth
                Console.WriteLine("go auth here " + url);
                Console.ReadLine();

                // If the user authed, let's get that token
                try {
                    token = client.GetAccessToken();
                }
                catch (Exception e) {
                    Console.WriteLine("Exception! " + e.Message);
                    return;
                }

                // save for later
                userToken = token.Token;
                userSecret = token.Secret;
                Settings.Default.userToken = userToken;
                Settings.Default.userSecret = userSecret;
                Settings.Default.Save();

            } else {
                client = new DropNet.DropNetClient(AppKey, AppSecret, userToken, userSecret);
                client.UseSandbox = true;
                client.GetToken();

                // get that token
                try {
                    token = client.GetAccessToken();
                } catch (Exception e) {
                    Console.WriteLine("Exception! " + e.Message);
                    return;
                }
            }

            var acctInfo = client.AccountInfo();
            Console.WriteLine(acctInfo.display_name);
            Console.ReadLine();
        }
    }
}

有效的代码:

using System;
using DropNet;

namespace DS_Uploader_DropBox {
    class Program {
        private const string AppKey = "my super secret app key";
        private const string AppSecret = "my super secret app secret";

        static void Main(string[] args) {
            DropNetClient client;
            DropNet.Models.UserLogin token;

            string userToken = Settings.Default.userToken;
            string userSecret = Settings.Default.userSecret;

            bool needAccessToken = (String.IsNullOrEmpty(userToken) || string.IsNullOrEmpty(userSecret));

            //needAccessToken = true;

            if (needAccessToken) {
                client = new DropNet.DropNetClient(AppKey, AppSecret);
                client.UseSandbox = true;
                client.GetToken();

                // Auth with dropbox
                var url = client.BuildAuthorizeUrl();

                // Prompt for user to auth
                Console.WriteLine("go auth here " + url);
                Console.ReadLine();

                // If the user authed, let's get that token
                try {
                    token = client.GetAccessToken();
                }
                catch (Exception e) {
                    Console.WriteLine("Exception! " + e.Message);
                    return;
                }

                // save for later
                userToken = token.Token;
                userSecret = token.Secret;
                Settings.Default.userToken = userToken;
                Settings.Default.userSecret = userSecret;
                Settings.Default.Save();

            } else {
                client = new DropNet.DropNetClient(AppKey, AppSecret, userToken, userSecret);
                client.UseSandbox = true;
            }

            var acctInfo = client.AccountInfo();
            Console.WriteLine(acctInfo.display_name);
            Console.ReadLine();
        }
    }
}

1 个答案:

答案 0 :(得分:2)

needAccessToken为false的代码路径中,您再次调用GetTokenGetAccessToken,分别尝试获取新的请求令牌和新的访问令牌。这是不必要的,因为您已经拥有并检索了userTokenuserSecret中的现有访问令牌。