Twitter4j身份验证错误

时间:2014-04-18 05:41:00

标签: java twitter4j

我需要获得推文的数量,关注者数和一个人的朋友数virat kohli.I使用Twitter4j jar我的代码看起来像这样

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.User;
import twitter4j.auth.OAuthAuthorization;
import twitter4j.conf.ConfigurationBuilder;


public class HomePage {
public static void main(String[] args) throws IOException, TwitterException {
    Properties props=new Properties();
    InputStream input=new FileInputStream(args[0]);
    props.load(input);
    ConfigurationBuilder conf=new ConfigurationBuilder();
         conf.setDebugEnabled(true).setOAuthConsumerKey(props.getProperty("ConsumerKey")).setOAuthConsumerSecret(props.getProperty("ConsumerSecret")).setOAuthAccessToken(props.getProperty("AccessToken")).setOAuthAccessTokenSecret(props.getProperty("AccessTokenSecret"));
    OAuthAuthorization auth = new OAuthAuthorization(conf.build());
     Twitter twitter = new TwitterFactory().getInstance(auth);
    // User user = twitter.verifyCredentials();
    User user=twitter.showUser("@imVkohli");
    System.out.println(user.getStatusesCount());
    System.out.println(user.getFriendsCount());
    System.out.println(user.getFollowersCount());




}

}

所有键都放在属性文件中,iam将属性文件的路径作为运行时参数传递。我得到了以下异常

Exception in thread "main" 401:Authentication credentials    (https://dev.twitter.com/pages/auth) were missing or incorrect. Ensure that you have set   valid consumer key/secret, access token/secret, and the system clock is in sync.
message - Could not authenticate you
code - 32

如果我试图获取我的时间线,那么我没有收到此错误。代码中是否有错误?

1 个答案:

答案 0 :(得分:0)

问题在于行

User user=twitter.showUser("@imVkohli");

它必须是屏幕名称而不是Twitter处理程序。守则必须

User user=twitter.showUser("imVkohli");