0

我遇到了第 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/编码方面的经验有限,但我不知道如何解决这个问题,因为我找不到谷歌上有很多有用的信息。我反编译了 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;
  }

日志4j:

 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);    
    }
}
4

0 回答 0