【问题标题】:Don`t able to save token from RestSharp OAuth request Windows Phone 8.1无法从 RestSharp OAuth 请求 Windows Phone 8.1 中保存令牌
【发布时间】:2015-06-03 18:04:57
【问题描述】:

我尝试使用 RestSharp 从 Trello 获取请求令牌。我仅在请求线程中获得令牌,但无法将其保存在我的应用程序的变量中。 有一些代码:

    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
    }

你有什么想法吗?

【问题讨论】:

    标签: c# oauth windows-phone-8.1 restsharp trello


    【解决方案1】:

    这个问题的解决方案是创建一个类,使我能够创建同步请求。 有这个类的代码:

    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(response)方法之后;执行,我的应用程序类中有令牌和令牌秘密。 我仍然相信这个问题一定有更好的解决方案,但这是我知道的唯一一个。

    【讨论】:

      猜你喜欢
      • 2012-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-27
      相关资源
      最近更新 更多