twitter4j缺少身份验证凭据

时间:2014-01-02 18:09:16

标签: java twitter twitter-oauth twitter4j

我有这段代码

    PrintWriter out = response.getWriter();
    String username="twitterapi";

   ConfigurationBuilder cb = new ConfigurationBuilder();
   cb.setDebugEnabled(true)
    .setOAuthConsumerKey("zRQs******yDLJuQ")
    .setOAuthConsumerSecret("W368******************DsWya56uk")
    .setOAuthAccessToken("124529**************EQ2XAfAQShzSWExwMoOS")
    .setOAuthAccessToken("UiH**********************k6UuCPveirdF");

 TwitterFactory tf = new TwitterFactory(cb.build());
 Twitter twitter = tf.getInstance();

    User user = null; 

         try {
             user = twitter.showUser(username); 
         } catch (TwitterException ex) {
             Logger.getLogger(Twitter_Final.class.getName()).log(Level.SEVERE, null, ex);
         }
    if (user.getStatus() != null) {
       out.println("@" + user.getScreenName() + " - " + user.getDescription());
    } else {
        // protected account
        out.println("@" + user.getScreenName());
    }

但是我得到了这个kwown错误消息“缺少身份验证凭据。有关详细信息,请参阅http://twitter4j.org/en/configuration.html。”

我在这里已经阅读了类似的问题,但我无法看到代码中的问题所在。你能吗?

1 个答案:

答案 0 :(得分:3)

问题出在您的ConfigurationBuilder,您设置了OAuthAccessToken两次,但从未OAuthAccessTokenSecret

只需将您的代码更改为:

ConfigurationBuilder cb = new ConfigurationBuilder();
           cb.setDebugEnabled(true)
            .setOAuthConsumerKey(YOUR_KEY)
            .setOAuthConsumerSecret(YOUR_SECRET)
            .setOAuthAccessToken(ACCESS_TOKEN)
            .setOAuthAccessTokenSecret(ACCESS_TOKEN_SECRET);

它会起作用!