首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >DropNet无法为后续请求保存访问令牌?

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

Stack Overflow用户
提问于 2015-04-08 16:37:15
回答 1查看 335关注 0票数 2

与我之前的许多人一样,我在启动应用程序之间未能成功地保存访问令牌。这个应用程序实际上只用于一个特定的dropbox帐户,尽管我试图使它更容易设置和更动态。

当然,如果设置是空的(初始运行),用户可以正确登录并授权应用程序,然后返回到它想要运行的应用程序。但是,在随后的运行中,当它从它使用的设置集合中获取令牌和秘密时,会非常失败。

未授权收到响应:预期将看到OK。HTTP响应是{"error":“无效签名。”}。

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

下面是密码!

代码语言:javascript
复制
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();
        }
    }
}

努力遵循的代码:

代码语言:javascript
复制
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();
        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-04-08 20:21:30

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29520473

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档