public async Task SaveToken(FlowFinanceToken flowFinanceToken) { Console.WriteLine("-> SaveToken <-"); var jsonSerializedToken = JsonConvert.SerializeObject(flowFinanceToken); var request = new HttpRequestMessage { Method = HttpMethod.Put, RequestUri = new Uri($"http://vbase.{this._environmentVariableProvider.Region}.vtex.io/{this._httpContextAccessor.HttpContext.Request.Headers[HEADER_VTEX_ACCOUNT]}/master/buckets/{this._applicationName}/{TOKEN_BUCKET}/files/{TOKEN_BUCKET}"), Content = new StringContent(jsonSerializedToken, Encoding.UTF8, APPLICATION_JSON) }; string authToken = this._httpContextAccessor.HttpContext.Request.Headers[HEADER_VTEX_CREDENTIAL]; if (authToken != null) { request.Headers.Add(AUTHORIZATION_HEADER_NAME, authToken); request.Headers.Add(VTEX_ID_HEADER_NAME, authToken); } var client = _clientFactory.CreateClient(); var response = await client.SendAsync(request); string responseContent = await response.Content.ReadAsStringAsync(); //Console.WriteLine($"-> SaveToken [{response.StatusCode}] {responseContent} <-"); }
public async Task GetToken() { bool needToken = true; string refreshToken = string.Empty; FlowFinanceToken flowFinanceToken = await _paymentRequestRepository.LoadToken(); if (flowFinanceToken != null) { this.accessToken = flowFinanceToken.AccessToken; refreshToken = flowFinanceToken.RefreshToken; //Console.WriteLine($"Has token? {!string.IsNullOrEmpty(flowFinanceToken.AccessToken)} Expires {flowFinanceToken.ExpiresAt}"); if (!string.IsNullOrEmpty(flowFinanceToken.AccessToken)) { if (flowFinanceToken.ExpiresAt > DateTime.Now) { needToken = false; } } } if (needToken) { //Console.WriteLine("Getting/Refreshing Token."); this.accessToken = await GetAccessToken(refreshToken); } }
public async Task <string> GetAccessToken(string refreshToken) { string token = string.Empty; Models.OAuthResponse.RootObject oAuthResponse = null; if (!string.IsNullOrEmpty(refreshToken)) { //Console.WriteLine($"Refreshing Token {refreshToken}"); // Try refreshing the old token oAuthResponse = await OAuthToken(refreshToken); //Console.WriteLine($"OAuthToken ---> {oAuthResponse.data.access_token} {oAuthResponse.data.expires_in} {oAuthResponse.data.refresh_token} {oAuthResponse.data.token_type}"); } if (oAuthResponse == null || oAuthResponse.data == null || string.IsNullOrEmpty(oAuthResponse.data.access_token)) { //Console.WriteLine("Getting Token"); // Get a new token oAuthResponse = await OAuthLogin(); //Console.WriteLine($"OAuthLogin ---> {oAuthResponse.data.access_token} {oAuthResponse.data.expires_in} {oAuthResponse.data.refresh_token} {oAuthResponse.data.token_type}"); } if (oAuthResponse != null && oAuthResponse.data != null && !string.IsNullOrEmpty(oAuthResponse.data.access_token)) { token = $"{oAuthResponse.data.token_type} {oAuthResponse.data.access_token}"; //Console.WriteLine($"GetAccessToken Success. {oAuthResponse.data.token_type} {oAuthResponse.data.expires_in}"); FlowFinanceToken flowFinanceToken = new FlowFinanceToken { AccessToken = token, ExpiresAt = DateTime.Now.AddSeconds(oAuthResponse.data.expires_in), RefreshToken = oAuthResponse.data.refresh_token }; await _paymentRequestRepository.SaveToken(flowFinanceToken); } else { Console.WriteLine("GetAccessToken Failure."); //throw new Exception("GetAccessToken Failure."); } return(token); }
public async Task <FlowFinanceToken> LoadToken() { Console.WriteLine("-> LoadToken <-"); var request = new HttpRequestMessage { Method = HttpMethod.Get, RequestUri = new Uri($"http://vbase.{this._environmentVariableProvider.Region}.vtex.io/{this._httpContextAccessor.HttpContext.Request.Headers[HEADER_VTEX_ACCOUNT]}/master/buckets/{this._applicationName}/{TOKEN_BUCKET}/files/{TOKEN_BUCKET}") }; string authToken = this._httpContextAccessor.HttpContext.Request.Headers[HEADER_VTEX_CREDENTIAL]; if (authToken != null) { request.Headers.Add(AUTHORIZATION_HEADER_NAME, authToken); request.Headers.Add(VTEX_ID_HEADER_NAME, authToken); } var client = _clientFactory.CreateClient(); var response = await client.SendAsync(request); string responseContent = await response.Content.ReadAsStringAsync(); //Console.WriteLine($"-> LoadToken [{response.StatusCode}] {responseContent} <-"); if (response.StatusCode == HttpStatusCode.NotFound) { return(null); } // A helper method is in order for this as it does not return the stack trace etc. response.EnsureSuccessStatusCode(); FlowFinanceToken flowFinanceToken = JsonConvert.DeserializeObject <FlowFinanceToken>(responseContent); return(flowFinanceToken); }
public Task SaveToken(FlowFinanceToken flowFinanceToken) { throw new System.NotImplementedException(); }