duanmangxie7131 2016-10-23 19:50
浏览 361

使用twitter API

I'm trying to create a small website which takes user's twitter username and retrieve the basic information from it like: screen name, created date, number of followers, etc... and display it. But I couldn't find a new helpful working tutorial or example of how to work with twitter API. Can someone please recommend something for me or just give some instructions of how should I start and work please? I prefer it to be done with Java or PHP.

Also I have a problem, when creating my application access key in twitter, I can't create the access token! the button is not shown! how can I solve this?

  • 写回答

1条回答 默认 最新

  • dsyrdwdbo47282676 2016-10-23 19:57
    关注

    You Can use below code-

        import twitter4j.Query;
        import twitter4j.QueryResult;
        import twitter4j.Status;
        import twitter4j.Twitter;
        import twitter4j.TwitterException;
        import twitter4j.TwitterFactory;
        import twitter4j.conf.ConfigurationBuilder;
    
        public class TwitterSearch {
    
            private static final String CATEGORY = "Mobile";
            private static final String SOURCE = "Twitter";
    
            public static void main(String ...args) throws TwitterException, IOException
            {
                ConfigurationBuilder cb = new ConfigurationBuilder();
                cb.setDebugEnabled(true)
                  .setOAuthConsumerKey(TwitterConfig.OATH_CONSUMER_KEY)
                  .setOAuthConsumerSecret(TwitterConfig.OATH_CONSUMER_SECRET)
                  .setOAuthAccessToken(TwitterConfig.OATH_ACCESS_TOKEN)
                  .setOAuthAccessTokenSecret(TwitterConfig.OATH_ACCESS_TOKEN_SECRET);
                TwitterFactory tf = new TwitterFactory(cb.build());
                Twitter twitter = tf.getInstance();
                System.out.println("Product to be searched " + args[0]);
                Query query = new Query(args[0]);
                query.setLang("en");
                query.setLocale("en_IN");
                query.setCount(100);
                QueryResult result = twitter.search(query);
                System.out.println("Output File "+ args[1]);
                System.out.println(result.getTweets());
                for(Status tweet : result.getTweets())
                {
                    System.out.println(tweet.getText());
                }
            }
    }
    

    result.getTweets() return a List, and you can iterate over that list and can print tweet.getText() to print tweet.

    Use this maven dependency-

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

    Refer below link for keys-

    https://dev.twitter.com/oauth/overview

    评论

报告相同问题?

悬赏问题

  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。