c# – 无法从RestSharp OAuth请求Windows Phone 8.1中保存令牌

我试图通过RestSharp从Trello获取Request Token.我只在请求线程中获得了令牌,但是无法将其保存在我的应用程序的变量中.
 有一些代码:

    private async void GetToken()
    {

        app.Client.Authenticator = OAuth1Authenticator.ForRequestToken(app.ConsumerKey, app.ConsumerSecret);
        var request = new RestRequest("OAuthGetRequestToken", Method.POST);
        app.Client.ExecuteAsync(request, HandleResponse);

    }

    private void HandleResponse(IRestResponse restResponse)
    {
        var Response = restResponse;
        MessageBox.Show(Response.Content);
        QueryString qs = new QueryString(Response.Content);
        app.OAuthToken = qs["oauth_token"];
        app.OAuthTokenSecret = qs["oauth_token_secret"];
        app.Verifier = qs["verifier"];
        MessageBox.Show(app.OAuthToken);       //got token here, but after
        MessageBox.Show(app.OAuthTokenSecret); //I don`t have anything in this variables
    }

你有什么想法吗?

解决方法:

解决此问题的方法是创建使我能够创建同步请求的类.
有这个类的代码:

public static class RestClientExtensions
{
    public static Task<IRestResponse> ExecuteTask(this IRestClient client, RestRequest request)
    {
        var TaskCompletionSource = new TaskCompletionSource<IRestResponse>();
        client.ExecuteAsync(request, (response, asyncHandle) =>
        {
            if (response.ResponseStatus == ResponseStatus.Error)
                TaskCompletionSource.SetException(response.ErrorException);
            else
                TaskCompletionSource.SetResult(response);
        });
        return TaskCompletionSource.Task;
    }
}

现在我的代码看起来像这样:

private async void GetToken()
    {

        app.Client.Authenticator = OAuth1Authenticator.ForRequestToken(app.ConsumerKey, app.ConsumerSecret);
        RestRequest request = new RestRequest("OAuthGetRequestToken", Method.POST);
        IRestResponse restResponse = await app.Client.ExecuteTask(request);
        HandleResponse(restResponse);
        GetAccesToken();
    }

    private void HandleResponse(IRestResponse response)
    {
        var Response = response;
        System.Diagnostics.Debug.WriteLine(Response.Content);
        QueryString qs = new QueryString(Response.Content);
        app.OAuthToken = qs["oauth_token"];
        app.OAuthTokenSecret = qs["oauth_token_secret"];
        System.Diagnostics.Debug.WriteLine(app.OAuthToken);
        System.Diagnostics.Debug.WriteLine(app.OAuthTokenSecret);
    }

并且在方法HandleResponse(响应)之后;执行,我在我的app类中有令牌和令牌秘密.
我仍然相信这个问题必须有更好的解决方案,但这是我所知道的唯一解决方案.

上一篇:使用Docker安装nexus3,搭建maven私服


下一篇:9.使用nexus3配置Python私有仓库