/// <summary> /// Refreshes the token asynchronous. /// </summary> /// <param name="clientSettings">ApiClientSettings needed for creating a proper refresh token HTTP post call.</param> /// <returns>Returns OAuth2AccessToken</returns> public static async Task <OAuth2AccessToken> RefreshTokenAsync(ApiClientSettings clientSettings) { var postUrl = DigiKeyUriConstants.TokenEndpoint; var content = new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>(OAuth2Constants.GrantType, OAuth2Constants.GrantTypes.RefreshToken), new KeyValuePair <string, string>(OAuth2Constants.ClientId, clientSettings.ClientId), new KeyValuePair <string, string>(OAuth2Constants.ClientSecret, clientSettings.ClientSecret), new KeyValuePair <string, string>(OAuth2Constants.GrantTypes.RefreshToken, clientSettings.RefreshToken), }); var httpClient = new HttpClient(); var response = await httpClient.PostAsync(postUrl, content); var responseString = await response.Content.ReadAsStringAsync(); var oAuth2AccessTokenResponse = OAuth2Helpers.ParseOAuth2AccessTokenResponse(responseString); _log.DebugFormat("RefreshToken: " + oAuth2AccessTokenResponse); clientSettings.UpdateAndSave(oAuth2AccessTokenResponse); return(oAuth2AccessTokenResponse); }
/// <summary> /// Refreshes the token asynchronous. /// </summary> /// <param name="clientSettings">ApiClientSettings needed for creating a proper refresh token HTTP post call.</param> /// <returns>Returns OAuth2AccessToken</returns> public static async Task <OAuth2AccessToken> RefreshTokenAsync(ApiClientSettings clientSettings) { var postUrl = DigiKeyUriConstants.TokenEndpoint; var content = new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>(OAuth2Constants.GrantType, OAuth2Constants.GrantTypes.RefreshToken), new KeyValuePair <string, string>(OAuth2Constants.GrantTypes.RefreshToken, clientSettings.RefreshToken), }); var httpClient = new HttpClient(); var clientIdConcatSecret = OAuth2Helpers.Base64Encode(clientSettings.ClientId + ":" + clientSettings.ClientSecret); httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", clientIdConcatSecret); var response = await httpClient.PostAsync(postUrl, content); var responseString = await response.Content.ReadAsStringAsync(); var oAuth2AccessTokenResponse = OAuth2Helpers.ParseOAuth2AccessTokenResponse(responseString); clientSettings.UpdateAndSave(oAuth2AccessTokenResponse); return(oAuth2AccessTokenResponse); }
/// <summary> /// Finishes authorization by passing the authorization code to the Token endpoint /// </summary> /// <param name="code">Code value returned by the RedirectUri callback</param> /// <returns>Returns OAuth2AccessToken</returns> public async Task <OAuth2AccessToken> FinishAuthorization(string code) { ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072; ServicePointManager.ServerCertificateValidationCallback = delegate { return(true); }; // Build up the body for the token request var body = new List <KeyValuePair <string, string> > { new KeyValuePair <string, string>(OAuth2Constants.Code, code), new KeyValuePair <string, string>(OAuth2Constants.RedirectUri, ClientSettings.RedirectUri), new KeyValuePair <string, string>(OAuth2Constants.ClientId, ClientSettings.ClientId), new KeyValuePair <string, string>(OAuth2Constants.ClientSecret, ClientSettings.ClientSecret), new KeyValuePair <string, string>(OAuth2Constants.GrantType, OAuth2Constants.GrantTypes.AuthorizationCode) }; // Request the token var requestMessage = new HttpRequestMessage(HttpMethod.Post, DigiKeyUriConstants.TokenEndpoint); var httpClient = new HttpClient { BaseAddress = DigiKeyUriConstants.BaseAddress }; requestMessage.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); requestMessage.Content = new FormUrlEncodedContent(body); Console.WriteLine("HttpRequestMessage {0}", requestMessage.RequestUri.AbsoluteUri); var tokenResponse = await httpClient.SendAsync(requestMessage).ConfigureAwait(false); var text = await tokenResponse.Content.ReadAsStringAsync(); // Check if there was an error in the response if (!tokenResponse.IsSuccessStatusCode) { var status = tokenResponse.StatusCode; if (status == HttpStatusCode.BadRequest) { // Deserialize and return model var errorResponse = JsonConvert.DeserializeObject <OAuth2AccessToken>(text); return(errorResponse); } // Throw error tokenResponse.EnsureSuccessStatusCode(); } // Deserializes the token response if successfull var oAuth2Token = OAuth2Helpers.ParseOAuth2AccessTokenResponse(text); _log.DebugFormat("FinishAuthorization: " + oAuth2Token); return(oAuth2Token); }
/// <summary> /// Refreshes the token asynchronous. /// </summary> /// <returns>Returns OAuth2AccessToken</returns> public async Task <OAuth2AccessToken> RefreshTokenAsync() { return(await OAuth2Helpers.RefreshTokenAsync(ClientSettings)); }