예제 #1
0
        private Models.OAuthResponse HandleApiResponse(System.Net.Http.HttpResponseMessage response, Models.TokenType tokenType)
        {
            var oAuthResponse = new Models.OAuthResponse();
            var result        = response.Content.ReadAsStringAsync().Result;

            if (response.StatusCode != System.Net.HttpStatusCode.OK)
            {
                oAuthResponse.ErrorMessage = result;
                var err = Newtonsoft.Json.JsonConvert.DeserializeObject <Models.Error>(result);
                oAuthResponse.Error = err;
                //if (!string.IsNullOrWhiteSpace(err?.ErrorDescription))
                //    throw err;
                //else
                //    throw new Models.ApiException((int)response.StatusCode, "Error calling GetItem: " + response.Content, response.Content.ReadAsStringAsync().Result);
                //log.Error("Error in fetching the token. Error:" + oAuthResponse.ErrorMessage);
            }
            else
            {
                var apiResponse = JsonConvert.DeserializeObject <Models.OAuthApiResponse>(result);
                oAuthResponse.TokenType    = apiResponse.TokenType;
                oAuthResponse.ErrorMessage = apiResponse.ErrorMessage;

                //Set AccessToken
                var accessToken = new Models.OAuthToken
                {
                    Token     = apiResponse.AccessToken,
                    ExpiresIn = apiResponse.ExpiresIn,
                    ExpiresOn = DateTime.Now.Add(new TimeSpan(0, 0, apiResponse.ExpiresIn)),
                    TokenType = tokenType
                };
                oAuthResponse.AccessToken = accessToken;

                //Set Refresh Token
                if (apiResponse.RefreshToken != null)
                {
                    var refreshToken = new Models.OAuthToken
                    {
                        Token     = apiResponse.RefreshToken,
                        ExpiresIn = apiResponse.RefreshTokenExpiresIn,
                        ExpiresOn = DateTime.Now.Add(new TimeSpan(0, 0, apiResponse.RefreshTokenExpiresIn)),
                    };
                    oAuthResponse.RefreshToken = refreshToken;
                }
            }
            //log.Info("Fetched the token successfully from API");
            return(oAuthResponse);
        }
예제 #2
0
        private Models.OAuthResponse fetchToken(string path, IEnumerable <KeyValuePair <string, string> > formParams, string basicAuthorization, Models.TokenType tokenType)
        {
            using (var request_ = new System.Net.Http.FormUrlEncodedContent(formParams))
            {
                request_.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("application/x-www-form-urlencoded");
                _httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", basicAuthorization);

                var response = _httpClient.PostAsync(path, request_).Result;

                return(HandleApiResponse(response, tokenType));
            }
        }