我遇到第26行的OAuthClientImpl.getInstance()方法问题。它正在吐出此错误消息:
Exception in thread "main" java.lang.ExceptionInInitializerError
at OAuth.main(OAuth.java:26)
Caused by: java.lang.ClassCastException: org.apache.logging.slf4j.SLF4JLoggerContext cannot be cast to org.apache.logging.log4j.core.LoggerContext
at org.apache.log4j.Logger.getLogger(Logger.java:41)
at com.etrade.etws.oauth.sdk.client.OAuthClientImpl.<init>(OAuthClientImpl.java:22)
at com.etrade.etws.oauth.sdk.client.OAuthClientImpl.<clinit>(OAuthClientImpl.java:24)
... 1 more
在我看来,问题是log4j和E * Trade OAuth jar之间的沟通问题,虽然我对Java /编码的总体经验有限,但我不知道如何解决这个问题,因为我无法在Google上找不到有用的信息。我反编译了E * Trade jar和log4j,发现了这段代码:
E *贸易:
public class OAuthClientImpl
implements IOAuthClient
{
private Logger logger = Logger.getLogger(getClass());
private static OAuthClientImpl instance = new OAuthClientImpl();
public static OAuthClientImpl getInstance()
{
return instance;
}
的Log4j:
public static Logger getLogger(final Class<?> clazz) {
return (Logger) Category.getInstance((LoggerContext) PrivateManager.getContext(), clazz);
}
以下是我的代码,它是E * Trade API示例代码,稍作调整。
import java.awt.Desktop;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import com.etrade.etws.account.Account;
import com.etrade.etws.account.AccountListResponse;
import com.etrade.etws.account.ETWSException;
import com.etrade.etws.oauth.sdk.client.IOAuthClient;
import com.etrade.etws.oauth.sdk.client.OAuthClientImpl;
import com.etrade.etws.oauth.sdk.common.Token;
import com.etrade.etws.sdk.client.ClientRequest;
import com.etrade.etws.sdk.client.Environment;
public class OAuth
{
public static void main(String[] args) throws IOException, URISyntaxException, com.etrade.etws.sdk.common.ETWSException
{
//Variables
IOAuthClient client = null;
ClientRequest request = null;
Token token = null;
String oauth_consumer_key = null; // Your consumer key
String oauth_consumer_secret = null; // Your consumer secret
String oauth_request_token = null; // Request token
String oauth_request_token_secret = null; // Request token secret
client = OAuthClientImpl.getInstance(); // Instantiate IOAUthClient
request = new ClientRequest(); // Instantiate ClientRequest
request.setEnv(Environment.SANDBOX); // Use sandbox environment
request.setConsumerKey(oauth_consumer_key); //Set consumer key
request.setConsumerSecret(oauth_consumer_secret);
token = client.getRequestToken(request); // Get request-token object
oauth_request_token = token.getToken(); // Get token string
oauth_request_token_secret = token.getSecret(); // Get token secret
}
public void OAuthVerify(IOAuthClient client, ClientRequest request) throws IOException, URISyntaxException, com.etrade.etws.sdk.common.ETWSException
{
String authorizeURL = null;
authorizeURL = client.getAuthorizeUrl(request); // E*TRADE authorization URL
URI uri = new java.net.URI(authorizeURL);
Desktop desktop = Desktop.getDesktop();
desktop.browse(uri);
}
}