private async Task <string> GetAlexaSecurityInfoAsync(Uri alexaUrl, string accessToken) { string bodyText = null; using (HttpClient httpClient = new HttpClient()) { httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); _logger?.LogDebug("Setting bearer token '{0}'", accessToken); httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); _logger?.LogDebug("Setting host header value '{0}'", alexaUrl.Host); httpClient.DefaultRequestHeaders.Host = alexaUrl.Host; _logger?.LogDebug("Executing GET against '{0}'", alexaUrl); HttpRequestMessage reqMessage = new HttpRequestMessage(HttpMethod.Get, alexaUrl); HttpResponseMessage respMessage = null; try { respMessage = await httpClient.SendAsync(reqMessage); } catch (Exception ex) { string errMsg = string.Format("Error calling {0}", alexaUrl); _logger?.LogError(ErrorEventCatalog.USER_SECURITYINVOCATION, ex, errMsg); throw new AlexaSecurityException(errMsg); } if (respMessage.StatusCode == System.Net.HttpStatusCode.OK) { bodyText = await respMessage.Content.ReadAsStringAsync(); _logger?.LogInformation($"Good response: {bodyText}"); } else { string alexaLogMessage = null; int httpStatus = (int)respMessage.StatusCode; //200 OK Successfully got the address associated with this deviceId. //204 No Content The query did not return any results. //401 Unauthorized authentication token timed out //403 Forbidden The authentication token is invalid or doesn't have access to the resource. //405 Method Not Allowed The method is not supported. //429 Too Many Requests The skill has been throttled due to an excessive number of requests. //500 Internal Error An unexpected error occurred. if (respMessage.StatusCode == System.Net.HttpStatusCode.NoContent) { // TODO throw an informative error here throw new AlexaSecurityException("No content returned", System.Net.HttpStatusCode.NoContent); } else { string respText = null; try { respText = await respMessage.Content.ReadAsStringAsync(); } catch (Exception ex) { alexaLogMessage = string.Format("Error deserializing response from {0}", alexaUrl); _logger?.LogError(alexaLogMessage, ex); throw new AlexaSecurityException(alexaLogMessage, respMessage.StatusCode, ex); } AlexaSecurityErrorResponse errResp = null; try { errResp = JsonConvert.DeserializeObject <AlexaSecurityErrorResponse>(respText); } catch (Exception ex) { string errMsg = "Could not deserialized Alexa Error Response"; _logger?.LogWarning(ErrorEventCatalog.ALEXA_ERROR_DESERIALIZATION, ex, errMsg); throw new AlexaSecurityException(errMsg, respMessage.StatusCode, ex); } if (errResp == null) { alexaLogMessage = string.Format("Error {0}: {1}", httpStatus, respText); _logger?.LogError(alexaLogMessage); throw new AlexaSecurityException(respText, respMessage.StatusCode); } else { alexaLogMessage = string.Format("Error {0}: Type: {1}, Message: {2}", httpStatus, errResp.Type, errResp.Message); _logger?.LogError(alexaLogMessage); throw new AlexaSecurityException(errResp, respMessage.StatusCode); } } } } return(bodyText); }
/// <summary> /// Initializes a new instance of the System.Exception class with a specified error message. /// </summary> /// <param name="message">The message that describes the error.</param> /// <param name="errorType">Type of the error, according to the Alexa error response message.</param> /// <param name="statusCode">Http status code of the Alexa Http response.</param> public AlexaSecurityException(AlexaSecurityErrorResponse errResponse, HttpStatusCode statusCode) : base(errResponse.Message) { _errorType = errResponse.Type; _code = errResponse.Code; _statusCode = statusCode; }