/// <summary> /// Revoke token. /// </summary> /// <param name="token"> the token </param> /// <exception cref="OAuthTokenException"> the o auth token exception </exception> /// <exception cref="JSONSerializationException"> the JSON serializer exception </exception> /// <exception cref="System.UriFormatException"> the URI syntax exception </exception> /// <exception cref="InvalidRequestException"> the invalid request exception </exception> /// <exception cref="System.InvalidOperationException"> if any other error occurred during the operation </exception> public virtual void RevokeToken(Token token) { // Create the request and send it To get the response/token. HttpRequest request = new HttpRequest(); request.Uri = new Uri(tokenURL); request.Method = HttpMethod.DELETE; // Set authorization header request.Headers["Authorization"] = "Bearer " + token.AccessToken; HttpResponse response = httpClient.Request(request); // Another error by not getting a 200 RequestResult if (response.StatusCode != HttpStatusCode.OK) { throw new OAuthTokenException("Token request failed with http error code: " + response.StatusCode); } httpClient.ReleaseConnection(); }
/// <summary> /// Request a token. /// /// Exceptions: /// - IllegalArgumentException : if Url is null or empty /// - InvalidTokenRequestException : if the token request is invalid /// - InvalidOAuthClientException : if the client information is invalid /// - InvalidOAuthGrantException : if the authorization Code or refresh token is invalid or /// expired, the redirect_uri does not match, or the hash Value does not match the client secret and/or Code /// - UnsupportedOAuthGrantTypeException : if the grant Type is invalid /// - OAuthTokenException : if any other error occurred during the operation /// </summary> /// <param name="url"> the URL (with request parameters) from which the token will be requested </param> /// <returns> the token </returns> /// <exception cref="OAuthTokenException"> the o auth token exception </exception> /// <exception cref="JSONSerializationException"> the JSON serializer exception </exception> /// <exception cref="System.UriFormatException"> the URI syntax exception </exception> /// <exception cref="InvalidRequestException"> the invalid request exception </exception> private Token RequestToken(string url) { // Create the request and send it To get the response/token. HttpRequest request = new HttpRequest(); request.Uri = new Uri(url); request.Method = HttpMethod.POST; request.Headers = new Dictionary<string, string>(); request.Headers["Content-Type"] = "application/x-www-form-urlencoded"; HttpResponse response = httpClient.Request(request); // Create a map of the response StreamReader inputStream = response.Entity.GetContent(); IDictionary<string, object> map = jsonSerializer.DeserializeMap(inputStream); httpClient.ReleaseConnection(); // Check for a error response and throw it. if (response.StatusCode != HttpStatusCode.OK && map.ContainsKey("error") && map["error"] != null) { string errorType = map["error"].ToString(); string errorDescription = map["message"] == null ? "" : (string)map["message"]; if ("invalid_request".Equals(errorType)) { throw new InvalidTokenRequestException(errorDescription); } else if ("invalid_client".Equals(errorType)) { throw new InvalidOAuthClientException(errorDescription); } else if ("invalid_grant".Equals(errorType)) { throw new InvalidOAuthGrantException(errorDescription); } else if ("unsupported_grant_type".Equals(errorType)) { throw new UnsupportedOAuthGrantTypeException(errorDescription); } else { throw new OAuthTokenException(errorDescription); } } // Another error by not getting a 200 RequestResult else if (response.StatusCode != HttpStatusCode.OK) { throw new OAuthTokenException("Token request failed with http error code: " + response.StatusCode); } // Create a token based on the response Token token = new Token(); object tempObj = map["access_token"]; token.AccessToken = tempObj == null ? "" : (string)tempObj; tempObj = map["token_type"]; token.TokenType = tempObj == null ? "" : (string)tempObj; tempObj = map["refresh_token"]; token.RefreshToken = tempObj == null ? "" : (string)tempObj; long? expiresIn = 0L; try { expiresIn = Convert.ToInt64(Convert.ToString(map["expires_in"])); } catch (Exception) { expiresIn = 0L; } token.ExpiresInSeconds = expiresIn; return token; }
/// <summary> /// Refresh token. /// /// Exceptions: /// - IllegalArgumentException : if token is null. /// - InvalidTokenRequestException : if the token request is invalid /// - InvalidOAuthClientException : if the client information is invalid /// - InvalidOAuthGrantException : if the authorization Code or refresh token is invalid or expired, /// the redirect_uri does not match, or the hash Value does not match the client secret and/or Code /// - UnsupportedOAuthGrantTypeException : if the grant Type is invalid /// - OAuthTokenException : if any other error occurred during the operation /// </summary> /// <param name="token"> the token To refresh </param> /// <returns> the refreshed token </returns> /// <exception cref="OAuthTokenException"> the o auth token exception </exception> /// <exception cref="JSONSerializationException"> the JSON serializer exception </exception> /// <exception cref="System.UriFormatException"> the URI syntax exception </exception> /// <exception cref="InvalidRequestException"> the invalid request exception </exception> public virtual Token RefreshToken(Token token) { // Create a map of the parameters IDictionary<string, string> @params = new Dictionary<string, string>(); @params["grant_type"] = "refresh_token"; @params["client_id"] = clientId; @params["refresh_token"] = token.RefreshToken; @params["redirect_uri"] = redirectURL; @params["hash"] = getHash(token.RefreshToken); // Generate the URL and get the token return RequestToken(GenerateURL(tokenURL, @params)); }