简体   繁体   中英

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.

My class for Authorizing Dropbox with DropNet is:

I have declared two properties; public string UserToken { get; set; } public string UserToken { get; set; } and 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.

Any help would be appreciated!

This is more of a .net question rather than a DropNet specific thing.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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