Exemplo n.º 1
0
        private Task <OAuthLoginResult> FetchToken(string tokenUri)
        {
            var httpClient = new HttpClient(new HttpClientHandler(), disposeHandler: true);

            return(httpClient.GetAsync(tokenUri)
                   .ContinueWith(async t =>
            {
                using (httpClient)
                {
                    if (t.IsFaulted || t.IsCanceled)
                    {
                        return new OAuthLoginResult(OAuthLoginState.UnknownError, null);
                    }

                    var response = t.Result;

                    if (!response.IsSuccessStatusCode)
                    {
                        return new OAuthLoginResult(OAuthLoginState.UnknownError, null);
                    }

                    var responseBody = await response.Content.ReadAsStreamAsync();

                    if (responseBody == null)
                    {
                        return new OAuthLoginResult(OAuthLoginState.UnknownError, null);
                    }

                    using (var reader = new StreamReader(responseBody))
                        using (var jsonReader = new JsonTextReader(reader))
                        {
                            var json = JObject.Load(jsonReader);
                            var result = OAuthResponse.FromJson(json);

                            return new OAuthLoginResult(OAuthLoginState.Success, result);
                        }
                }
            })
                   .Unwrap());
        }
Exemplo n.º 2
0
 public OAuthLoginResult(OAuthLoginState state,
                         OAuthResponse tokenResponse)
 {
     this.State         = state;
     this.TokenResponse = tokenResponse;
 }