编程语言
首页 > 编程语言> > java-twitter4j中缺少身份验证凭据

java-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());
    }

但是我得到了众所周知的错误消息“缺少身份验证凭据.有关详细信息,请参见http://twitter4j.org/en/configuration.html.”

我在这里阅读过类似的问题,但是我看不到代码中的问题在哪里.你是否可以?

解决方法:

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

只需将代码更改为:

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

会的!

标签:twitter4j,twitter,twitter-oauth,java
来源: https://codeday.me/bug/20191122/2058675.html