使用 DropNet 从 Dropbox 存储用户令牌

Store user token from Dropbox using DropNet

我正在尝试在我的应用程序获得 Dropbox 授权后存储用户令牌,这样当我打开一个不同的表单(将具有拖放功能)时,用户就不必授权该应用程序再次将能够执行上传到 Dropbox 的功能。

我使用 DropNet 授权 Dropbox 的 class 是:

我已经声明了两个属性; public string UserToken { get; set; }public string UserSecret { get; set; }

    string appKey = "APP_KEY";
    string appSecret = "APP_SECRET";

    public bool LinkDrpbox()
    {
        bool dropboxLink = false;

        Authenticate(
           url =>
           {
               var proc = Process.Start("iexplore.exe", url);
               proc.WaitForExit();
               Authenticated(
                   () =>
                   {
                       dropboxLink = true;
                   },
                   exc => ShowException(exc));

           },
           ex => dropboxLink = false);

        if (dropboxLink)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    private DropNetClient _Client;
    public DropNetClient Client
    {
        get
        {
            if (_Client == null)
            {
                _Client = new DropNetClient(appKey, appSecret);

                if (IsAuthenticated)
                {
                    _Client.UserLogin = new UserLogin
                    {
                        Token = UserToken,
                        Secret = UserSecret
                    };
                }

                _Client.UseSandbox = true;
            }
            return _Client;
        }
    }

    public bool IsAuthenticated
    {
        get
        {
            return UserToken != null &&
                UserSecret != null;
        }
    }

    public void Authenticate(Action<string> success, Action<Exception> failure)
    {
        Client.GetTokenAsync(userLogin =>
        {
            var url = Client.BuildAuthorizeUrl(userLogin);
            if (success != null) success(url);
        }, error =>
        {
            if (failure != null) failure(error);
        });
    }

    public void Authenticated(Action success, Action<Exception> failure)
    {
        Client.GetAccessTokenAsync((accessToken) =>
        {
            UserToken = accessToken.Token;
            UserSecret = accessToken.Secret;

            if (success != null) success();
        },
        (error) =>
        {
            if (failure != null) failure(error);
        });
    }

    private void ShowException(Exception ex)
    {
        string error = ex.ToString();
    }
}

我可以授权我的应用程序,但不确定如何保存访问令牌。我假设在 app.config 文件中但不确定。

如有任何帮助,我们将不胜感激!

这更像是一个 .net 问题,而不是 DropNet 特定的问题。

看看这个答案我不同意使用注册表,但其他两个很好。

您只需在启动应用程序时从存储它的位置加载它,并将其设置在您的 DropNetClient 实例上。