简体   繁体   English

DropNet登录测试

[英]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 所以我只是在为正在开发的新.net应用程序测试DropNet客户端,而我只是想通过这样做登录

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. 单击按钮2时显示错误消息。

I've also declared my DropNetClient _Client = new DropNetClient("API KEY", "API SECRET"); 我还声明了我的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. 这是通过利用每个异步调用的成功Action来完成的,对于Process.Start()而言,使用Process.WaitForExit()进行调用,然后再继续。

Finally, Process.Start() must explicitly use iexplore as not all browsers return a process (Chrome for example). 最后,由于并非所有浏览器都返回一个进程(例如Chrome),因此Process.Start()必须显式使用iexplore。

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

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

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