0

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

我使用 DropNet 授权 Dropbox 的课程是:

我已经声明了两个属性;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文件中,但不确定。

任何帮助,将不胜感激!

4

1 回答 1

1

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

看看这个答案https://stackoverflow.com/a/3032538/75946我不同意使用注册表,但其他两个很好。

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

于 2015-05-14T14:11:34.243 回答