public static async Task <string> RefreshAccessToken(this CustomHttpClient client, string refreshToken) { var refreshMessage = new HttpRequestMessage(HttpMethod.Post, "/oauth2/v4/token") { Content = new FormUrlEncodedContent(new KeyValuePair <string, string>[] { //new KeyValuePair<string, string>("client_id", _configuration["Authentication:Google:ClientId"]), //new KeyValuePair<string, string>("client_secret", _configuration["Authentication:Google:ClientSecret"]), //new KeyValuePair<string, string>("refresh_token", refreshToken), //new KeyValuePair<string, string>("grant_type", "refresh_token") }) }; var response = await client.SendAsync(refreshMessage); if (response.IsSuccessStatusCode) { //var tokenResponse = await response.Content.ReadAsAsync<TokenResponse>(); var tokenResponse = new TokenResponse(); return(tokenResponse.AccessToken); } // return null if we cannot request a new token return(null); }
//public static CustomHttpClient Set //public static AsyncRetryPolicy CreateTokenRefreshPolicy(this CustomHttpClient client, Func<DelegateResult<HttpResponseMessage>, int, Context, Task> refreshToken) //{ // var policy = Policy // .Handle<Exception>(ex => ex is HttpRequestException) // //.HandleResult(message => message.StatusCode == HttpStatusCode.Unauthorized) // .RetryAsync(1, async (result, retryCount, context) => // { // await refreshToken(result, retryCount, context); // }); // return policy; //} public static AsyncRetryPolicy CreateTokenRefreshPolicy(this CustomHttpClient client, Func <Exception, int, Context, Task> refreshToken) { var policy = Policy .Handle <Exception>(ex => ex is HttpRequestException) //.HandleResult(message => message.StatusCode == HttpStatusCode.Unauthorized) .RetryAsync(1, async(result, retryCount, context) => { await refreshToken(result, retryCount, context); }); return(policy); }
public static CustomHttpClient AddPolicy(this CustomHttpClient client, IAsyncPolicy policy) { client.Policies.Add(policy); return(client); }