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?

Answers


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

Need Your Help

Android XML pull parser cannot get attributes

android xml parsing pull

I am trying to load this xml file into an Android app. I got everything working but for some reason I can't get it to load the id.

Improving Indy HTTP client performance on older systems, especially laptops (Delphi 6)?

delphi http sockets indy robot

I have an application written in Delphi 6 that uses the Indy 9.0.18 HTTP client component to drive a robot. If I recall correctly when I did the Indy install, version 10 and newer does not work with

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.