Twitter4J - 无需登录即可查找个人资料详情

时间:2010-05-08 13:10:14

标签: java api twitter twitter4j

我已经使用Twitter4J了很长一段时间了,但我似乎无法找到这个特殊的功能。

我希望能够搜索某个名称,当某个用户在Twitter上时,我想要检索基本信息,例如推文,粉丝(就像你可以通过http访问) - 但是怎么做就是在Twitter4J ?代码示例或源代码都无法帮助我。

谢谢,

William van Doorn

3 个答案:

答案 0 :(得分:3)

请看以下内容: Twitter4J.api.UserMethods有你寻求的方法。 Twitter类直接实现它。

答案 1 :(得分:2)

我认为你应该使用twitter4j库,获取Access Token然后创建twitter的对象 然后调用showuser()它将返回用户对象然后在用户对象的帮助下你将获得登录用户的所有信息我将给你一个示例代码。

    AccessToken a = new AccessToken(token,secret);
    Twitter twitter = new TwitterFactory().getInstance();
    twitter.setOAuthConsumer(Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET);
    twitter.setOAuthAccessToken(a);

    int i=twitter.getId();
    User u=twitter.showUser(i);
    System.out.println("user name"+u.getName());

答案 2 :(得分:0)

我的POM.XML

...
  <dependencies>
      <dependency>
           <groupId>org.twitter4j</groupId>
           <artifactId>twitter4j-core</artifactId>
           <version>[4.0,)</version>
       </dependency>
  </dependencies>
...

我的班级考试

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

    public final class GetAccountSettings {

        private final String twitter_consumer_key = "oKu1emQJUGArI6kFNXNTCZ111";
        private final String twitter_consumer_secret = "g3EzljGP91AuqjBtIRjGFbKVEAg5cpxH96D6rWIgk4MbhK5111";
        private final static String oauth_token = "2847960911-LYoKllWcTo3VxZMdcLktWDPbLaeOywwrkf2W111";
        private final static String oauth_token_secret = "JoCajDpKvvzzOfsfQpar8aqIDmspyrGehvdj3YsCAp111";

        public static void main(String[] args) {
        try {

            GetAccountSettings getAccountSettings = new GetAccountSettings();

            // Twitter Conf.
            ConfigurationBuilder cb = new ConfigurationBuilder();
            cb.setDebugEnabled(true).setOAuthConsumerKey(getAccountSettings.twitter_consumer_key).setOAuthConsumerSecret(getAccountSettings.twitter_consumer_secret)
                .setOAuthAccessToken(getAccountSettings.oauth_token).setOAuthAccessTokenSecret(getAccountSettings.oauth_token_secret);

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

            User user = twitter.showUser("MR_Camaleon");
            System.out.println("\n ***************************    INFORMATION TWITTER PROFILE " + user.getScreenName() + "    ************************* \n\n");
            System.out.println("      Id:                    " + user.getId() + " \n");
            System.out.println("      Screen name:           " + user.getScreenName() + " \n");
            System.out.println("      Name:                  " + user.getName() + " \n");
            System.out.println("      Description:           " + user.getDescription() + " \n");
            System.out.println("      Image profile:         " + user.getProfileImageURL() + " \n");
            System.out.println("      Follorwers:            " + user.getFollowersCount() + " \n");
            System.out.println("      Friends:               " + user.getFriendsCount() + " \n");
            System.out.println("      Created date:          " + user.getCreatedAt() + " \n");
            System.out.println("      Language:              " + user.getLang() + " \n");
            System.out.println("      Time zone:             " + user.getTimeZone() + " \n\n");
            System.out.println(" ******************************************************************************************************");

        } catch (TwitterException te) {
            te.printStackTrace();
            System.out.println("Failed to get account settings: " + te.getMessage());
            System.exit(-1);
        }
        }
    }