twitter4j getOauthAccessToken给出异常

时间:2011-09-04 22:58:26

标签: java google-app-engine twitter4j

我试图在我的appengine项目中使用twitter4j获取用户的访问令牌。我的代码在

之下
    HttpSession session = req.getSession();
    String tokenCopy = (String) session
            .getAttribute(Constants.TOKEN_SESSION_KEY);
    String tokenSecretCopy = (String) session
            .getAttribute(Constants.TOKENSECRET_SESSION_KEY);

    String oauth_token = req.getParameter("oauth_token");
    //The above is the same as the tokenCopy, verified by printing.
    String oauth_verifier = req.getParameter("oauth_verifier");

    Twitter twitter = new TwitterFactory().getInstance();
    twitter.setOAuthConsumer(Constants.CONSUMER_KEY,
            Constants.CONSUMER_SECRET);

    String extraHTML = "NOTHING\n";

    try {
        AccessToken accessToken;
        accessToken = twitter
                .getOAuthAccessToken(tokenCopy, oauth_verifier);
        // accessToken = twitter.getOAuthAccessToken(oauth_verifier);
        // accessToken = twitter.getOAuthAccessToken();

        String accessTokenStr = accessToken.getToken();
        String accessTokenSecret = accessToken.getTokenSecret();
        extraHTML = "<p>accessToken = " + accessTokenStr
                + ", accessTokenSecret = " + accessTokenSecret + "</p>\n";

        // TODO latertwitter.setOAuthAccessToken(accessToken);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        extraHTML = "<pre>" + e.getMessage() + "</pre>\n";
    }

可悲的是,当我的代码运行时,我总是得到异常

401:Authentication credentials (https://dev.twitter.com/docs/auth) were missing or
incorrect. Ensure that you have set valid conumer key/secret, access token/secret,
and the system clock in in sync.

我知道我的CONSUMER_KEYCONSUMER_SECRET是正确的(我用它们来创建twitter身份验证网址并且效果很好)。我在这里错过了什么?我应该将其他内容传递给getOAuthAccessToken()吗?

感谢。

1 个答案:

答案 0 :(得分:2)

我正在使用RequestToken对象,而不是String,其他所有内容都相同。

我的意思是,当我准备身份验证网址时,我正在做以下事情:

RequestToken requestToken = twitter.getOAuthRequestToken(callbackUrl);
session.setAttribute(Constants.TOKEN_SESSION_KEY, requestToken);

而不是代码的第2行,它将是:

RequestToken tokenCopy = (RequestToken) session.getAttribute(Constants.TOKEN_SESSION_KEY);

其他一切看起来都是正确的。