简体   繁体   中英

Load DropNet user token and secret from a database in a C# WinForms application

I wonder if anyone can help. I'm using DropNet client and I've successfully authorized the app with Dropbox and I've stored the user token and secret within a SQL database so I can access them again as follows:

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

            UserAccountManagerBLL accBll = new UserAccountManagerBLL();
            accBll.RememberMe(UserToken, UserSecret, Email);

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

What I want to do is load the UserToken and UserSecret upon loading another form so I can drag and drop files and upload to Dropbox without having to authenticate the app with Dropbox all over again. Here's how I'm loading the token and secret:

private void DropTray_Load(object sender, EventArgs e)
{
        DropboxAccess dAccess = new DropboxAccess();

        UserAccountManagerBLL accMan = new UserAccountManagerBLL();

        UserToken = accMan.GetToken(Email);
        UserSecret= accMan.GetSecret(Email);

        if (UserToken == null && UserSecret == null)
        {
            MessageBox.Show(returnError());
        }
        else
        {
            Rectangle workingArea = Screen.GetWorkingArea(this);
            this.Location = new Point(workingArea.Right - Size.Width,
                                      workingArea.Bottom - Size.Height);

            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
        }
}

This method is used to get the token

public string GetToken(string eMail)
{
        using (cxn = new SqlConnection(this.ConnectionString))
        {
            using (cmd = new SqlCommand("spGetDetails", cxn))
            {
                cmd.CommandType = CommandType.StoredProcedure;

                cxn.Open();
                SqlDataReader dReader = cmd.ExecuteReader();

                while (dReader.Read())
                {
                    Utoken = dReader["UserToken"].ToString();
                    break;
                }
                dReader.Close();
                cxn.Close();
            }
        }

        return Utoken;
}

Same for the secret

And once I have them, I have two properties that will hold these values upon page load:

    public string UserToken { get; set; }
    public string UserSecret { get; set; }

The problem is I don't know how to get DropNet to recognise these values I've loaded from the database and I can just start to drag and drop files!?

Update: here's where _Client gets the user token and secret for DropNet:

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;
        }
    }

If anyone want's it, here's my repository....

SO I was almost there. It turns out all I had to do was create a new instance of DropNet client as opposed to a new UserLogin instance on the page load event!

        UserAccountManagerBLL accMan = new UserAccountManagerBLL();

        UserToken = accMan.GetToken(Email);
        UserSecret = accMan.GetSecret(Email);

        _Client = new DropNetClient(appKey, appSecret, UserToken, UserSecret);

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