简体   繁体   English

使用DropNet从Dropbox存储用户令牌

[英]Store user token from Dropbox using DropNet

I'm trying to store a user token once my application has been authorized with Dropbox so that when I open a different form (which will have drag and drop functionality), the user won't have to authorize the app again and will be able to perform upload functions to Dropbox. 一旦我的应用程序已被Dropbox授权,我将尝试存储用户令牌,以便当我打开其他表单(具有拖放功能)时,用户无需再次授权应用程序,并且能够执行向Dropbox的上传功能。

My class for Authorizing Dropbox with DropNet is: 我使用DropNet授权Dropbox的课程是:

I have declared two properties; 我已经声明了两个属性; public string UserToken { get; set; } public string UserToken { get; set; } and public string UserSecret { get; set; } public string UserToken { get; set; }public string UserSecret { 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();
    }
}

I am able to authorize my app but am unsure how to save the access token. 我可以授权我的应用程序,但不确定如何保存访问令牌。 I'm presuming in app.config file but not sure. 我想在app.config文件中,但不确定。

Any help would be appreciated! 任何帮助,将不胜感激!

This is more of a .net question rather than a DropNet specific thing. 这更多是.net问题,而不是DropNet特定问题。

Have a look at this answer https://stackoverflow.com/a/3032538/75946 I don't agree with using the registry but the other 2 are good. 看看这个答案https://stackoverflow.com/a/3032538/75946我不同意使用注册表,但是其他两个都很好。

You will just need to load it from where ever you store it when you start up the app and set it on your DropNetClient instance. 您只需要在启动应用程序并将其设置在DropNetClient实例上时从存储位置加载它即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM