简体   繁体   中英

DropNet Login Test

So I'm just testing out the DropNet client for a new .net application I'm developing and I'm just trying to login by doing this

private void button1_Click(object sender, EventArgs e)
    {
        _client.GetTokenAsync((userLogin) =>
            {
            },
            (error) =>
            {
                MessageBox.Show("Error getting Token");
            });

        var uLogin = _client.GetToken();

        var url = _client.BuildAuthorizeUrl(uLogin);

        System.Diagnostics.Process.Start(url);

        _client.GetAccessTokenAsync((accessToken) =>
            {

            },
            (error) =>
            {
                MessageBox.Show("Error getting Access Token");
            });


    }

And then trying to display my Account information in a message box by doing the following:

private void button2_Click(object sender, EventArgs e)
    {
        _client.AccountInfoAsync((accountInfo) =>
            {
                MessageBox.Show(accountInfo.ToString());
            },
            (error) =>
            {
                MessageBox.Show("Error displaying data");
            }
            );
    }

The error message displays when button 2 is clicked.

I've also declared my DropNetClient _Client = new DropNetClient("API KEY", "API SECRET"); at the top of the class.

Any suggestions?

You're treating asynchronous calls as though they're synchronous.

Each async call needs to wait for the previous to return before continuing.

This is done by making use of the success Action of each Async call and in the case of Process.Start() using the Process.WaitForExit() call before continuing.

Finally, Process.Start() must explicitly use iexplore as not all browsers return a process (Chrome for example).

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

    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 button1_Click(object sender, EventArgs e)
    {
        Authenticate(
               url => {
                   var proc = Process.Start("iexplore.exe", url);
                   proc.WaitForExit(); 
                    Authenticated(
                        () =>
                        {
                            MessageBox.Show("Authenticated");
                        },
                        exc => ShowException(exc));

               },
               ex => ShowException(ex));
    }

    private void button2_Click(object sender, EventArgs e)
    {
        Client.AccountInfoAsync((a) =>
        {
            MessageBox.Show(a.display_name);
        },
        ex => ShowException(ex));
    }

    private void ShowException(Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }

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