/// <summary> /// Internal method to perform a web request. /// Handles the case when API returns 403 forbidden due to expired token /// </summary> /// <param name="request">The request</param> /// <returns>The response</returns> private OAuth2ClientCredentialsGrantResponse PerformRequest(string resource, string[] urlData, Dictionary<string, string> bodyData, object bodyObject, string verb) { for (int i = 0; i < 2; i++) { HttpWebRequest request = MakeRequest(resource, urlData, bodyData, bodyObject, verb); if (request == null && !string.IsNullOrEmpty(lastErrorAsString)) { OAuth2ClientCredentialsGrantResponse resp = (OAuth2ClientCredentialsGrantResponse) JsonConvert.DeserializeObject(lastErrorAsString, typeof(OAuth2ClientCredentialsGrantResponse)); resp.response = null; lastErrorAsString = null; return resp; } HttpWebResponse response = null; try { response = (HttpWebResponse)request.GetResponse(); OAuth2ClientCredentialsGrantResponse toRet = new OAuth2ClientCredentialsGrantResponse(); toRet.errorCode = 0; toRet.errorType = 0; toRet.errorMessage = null; toRet.response = response; return toRet; } catch (WebException ex) { if (ex.Response != null) { StreamReader sr = new StreamReader(ex.Response.GetResponseStream()); lastErrorAsString = sr.ReadToEnd(); OAuth2ClientCredentialsGrantResponse resp = (OAuth2ClientCredentialsGrantResponse)JsonConvert.DeserializeObject(lastErrorAsString, typeof(OAuth2ClientCredentialsGrantResponse)); if (resp.errorCode == (int)OAuth2ClientCredentialsGrantError.InvalidAccessToken) { //We need to renew the token accessToken = null; lastErrorAsString = null; } else { resp.response = response; return resp; } } else { OAuth2ClientCredentialsGrantResponse resp = new OAuth2ClientCredentialsGrantResponse(); resp.errorCode = (int) OAuth2ClientError.ErrorGettingResponseFromServer; resp.errorType = (int)OAuth2ClientCredentialsGrant.OAuth2ErrorType.ApiClientError; resp.response = null; return resp; } } } return null; }
/// <summary> /// Invalidates the current authentication token if it exists /// </summary> /// <returns></returns> public OAuth2ClientCredentialsGrantResponse InvalidateToken() { if (string.IsNullOrEmpty(accessToken)) { OAuth2ClientCredentialsGrantResponse response = new OAuth2ClientCredentialsGrantResponse(); response.errorCode = (int) OAuth2ClientError.TokenDoesNotExist; response.errorType = (int) OAuth2ErrorType.ApiClientError; response.errorMessage = "No token to delete"; response.response = null; return response; } //We need to invalidate the token HttpWebResponse lastResponse = null; string url = MakeUrl("invalidate_token", null); HttpWebRequest request = MakeAuthorizedRequest(url, true); try { lastResponse = (HttpWebResponse)request.GetResponse(); if (lastResponse.StatusCode == HttpStatusCode.OK) { accessToken = null; OAuth2ClientCredentialsGrantResponse response = new OAuth2ClientCredentialsGrantResponse(); response.errorCode = 0; response.errorType = 0; response.errorMessage = null; response.response = lastResponse; return response; } else { OAuth2ClientCredentialsGrantResponse response = new OAuth2ClientCredentialsGrantResponse(); response.errorCode = (int) OAuth2ClientError.InvalidateTokenFailed; response.errorType = (int) OAuth2ErrorType.ApiClientError; response.errorMessage = "Could not invalidate token"; response.response = lastResponse; return response; } } catch (WebException ex) { if (ex.Response != null) { StreamReader sr = new StreamReader(ex.Response.GetResponseStream()); string err = sr.ReadToEnd(); OAuth2ClientCredentialsGrantResponse response = (OAuth2ClientCredentialsGrantResponse)JsonConvert.DeserializeObject(err, typeof(OAuth2ClientCredentialsGrantResponse)); response.response = null; return response; } else { OAuth2ClientCredentialsGrantResponse resp = new OAuth2ClientCredentialsGrantResponse(); resp.errorCode = (int)OAuth2ClientError.ErrorGettingResponseFromServer; resp.errorType = (int)OAuth2ClientCredentialsGrant.OAuth2ErrorType.ApiClientError; resp.response = null; return resp; } } }