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 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line