public void RefreshAccessToken(OAuthToken token, OAuthApplication oAuthApplication) { var refreshToken = token.RefreshToken; var client = new TokenClient(tokenUrl, oAuthApplication.ClientID, oAuthApplication.ClientSecret, AuthenticationStyle.PostValues); var tokenResponse = client.RequestRefreshTokenAsync(refreshToken).Result; if (!tokenResponse.IsError) { FillTokenFromTokenResponse(tokenResponse, token); return; } string error = tokenResponse.Error; string error_description = ""; try{ error_description = (((Newtonsoft.Json.Linq.JValue)tokenResponse.Json["error_description"]).Value).ToString(); }catch (Exception) {} Exceptions.VATAPIInvalidToken e = new Exceptions.VATAPIInvalidToken(Model.error.IMPOSSIBLE_TO_REFRESH_TOKEN); e.Data.Add("AccessToken", token.AccessToken); e.Data.Add("RefreshToken", token.RefreshToken); e.Data.Add("Raw", tokenResponse.Raw); throw e; }
public void RefreshAccessToken(OAuthSettings oAuthApplication, OAuthToken token) { if (token.RefreshToken == null) { throw new Exceptions.VATAPIInvalidToken(Model.error.REFRESH_TOKEN_IS_MISSING); } var request = new RefreshTokenRequest() { ClientId = oAuthApplication.ClientID, ClientSecret = oAuthApplication.ClientSecret, RefreshToken = token.RefreshToken, RequestUri = new Uri(tokenUrl) }; using (HttpClient httpClient = new HttpClient()) { Task <TokenResponse> T = Task.Run(() => httpClient.RequestRefreshTokenAsync(request)); TokenResponse tokenResponse = T.Result; if (!tokenResponse.IsError) { FillTokenFromTokenResponse(tokenResponse, token); return; } string error = tokenResponse.Error; string error_description = ""; try { error_description = (((Newtonsoft.Json.Linq.JValue)tokenResponse.Json["error_description"]).Value).ToString(); } catch (Exception) { } Exceptions.VATAPIInvalidToken e = new Exceptions.VATAPIInvalidToken(Model.error.IMPOSSIBLE_TO_REFRESH_TOKEN); e.Data.Add("AccessToken", token.AccessToken); e.Data.Add("RefreshToken", token.RefreshToken); e.Data.Add("Raw", tokenResponse.Raw); throw e; } }