Twitter Api检测到我的推文被转发或受欢迎

时间:2014-08-20 05:15:03

标签: java twitter twitter4j twitter-streaming-api

是否有可能检测到有人转发或偏爱我的状态? twitter api是否为用户的推文RTd或Favourited提供了听众?

特别是,我正在寻找允许我这样做的twitter4j。​​

2 个答案:

答案 0 :(得分:1)

是的,但有一个警告 - 这是一个未记录且不受支持的API调用

https://api.twitter.com/1.1/activity/about_me.json

如果使用官方 Twitter客户端的API密钥进行签名,则只能成功调用该资源。即Twitter for iPhone / Android /等。

如何获得这些键是留下来的创造力练习。 Twitter禁止使用此类未记录的API。也禁止使用他人的密钥。

答案 1 :(得分:1)

Ok在彻底搜索Twitter Streaming Api之后,这里是我使用过的代码:

对于收藏夹,我使用UserStreamListener来收听其他人收藏的身份验证用户的推文(请参阅onFavorite()):

        TwitterStream twitterStream = new TwitterStreamFactory(new ConfigurationBuilder().setJSONStoreEnabled(true).build()).getInstance();

        twitterStream.setOAuthConsumer(StaticConstants.API_KEY, StaticConstants.API_SECRET);
        AccessToken token = new AccessToken(StaticConstants.TOKEN_KEY, StaticConstants.TOKEN_SECRET);
        twitterStream.setOAuthAccessToken(token);

        UserStreamListener listener = new UserStreamListener() {
            public void onStatus(Status status) {
                System.out.println("Status: " + status.getText());
                //do your action here
            }

            @Override
            public void onDeletionNotice(StatusDeletionNotice sdn) {
            }

            @Override
            public void onTrackLimitationNotice(int i) {
            }

            @Override
            public void onScrubGeo(long l, long l1) {
            }

            @Override
            public void onStallWarning(StallWarning sw) {
            }

            @Override
            public void onException(Exception excptn) {
            }

            @Override
            public void onDeletionNotice(long l, long l1) {
            }

            @Override
            public void onFriendList(long[] longs) {
            }

            @Override
            public void onFavorite(User user, User user1, Status status) {
                System.out.println("User:"+user.getScreenName());
                System.out.println("User1 :"+user1.getScreenName());
                System.out.println("Status: "+status.getText());
            }

            @Override
            public void onUnfavorite(User user, User user1, Status status) {
            }

            @Override
            public void onFollow(User user, User user1) {
            }

            @Override
            public void onDirectMessage(DirectMessage dm) {
            }

            @Override
            public void onUserListMemberAddition(User user, User user1, UserList ul) {
            }

            @Override
            public void onUserListMemberDeletion(User user, User user1, UserList ul) {
            }

            @Override
            public void onUserListSubscription(User user, User user1, UserList ul) {
            }

            @Override
            public void onUserListUnsubscription(User user, User user1, UserList ul) {
            }

            @Override
            public void onUserListCreation(User user, UserList ul) {
            }

            @Override
            public void onUserListUpdate(User user, UserList ul) {
            }

            @Override
            public void onUserListDeletion(User user, UserList ul) {
            }

            @Override
            public void onUserProfileUpdate(User user) {
            }

            @Override
            public void onBlock(User user, User user1) {
            }

            @Override
            public void onUnblock(User user, User user1) {
            }

        };
        twitterStream.addListener(listener);
        twitterStream.user();

为了听取其他用户为特定用户(不一定是身份验证用户)转发的转推,我使用了StatusListener,如下所示:

          StatusListener statusListner=new StatusListener() {

            @Override
            public void onStatus(Status status) {
                System.out.println("Status Recvd: "+status.getText());
            }

            @Override
            public void onDeletionNotice(StatusDeletionNotice sdn) {
                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            }

            @Override
            public void onTrackLimitationNotice(int i) {
                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            }

            @Override
            public void onScrubGeo(long l, long l1) {
                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            }

            @Override
            public void onStallWarning(StallWarning sw) {
                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            }

            @Override
            public void onException(Exception excptn) {
                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            }
        };

        TwitterStream twitterStream2 = new TwitterStreamFactory(new ConfigurationBuilder().setJSONStoreEnabled(true).build()).getInstance();

        twitterStream2.setOAuthConsumer(StaticConstants.API_KEY, StaticConstants.API_SECRET);
        AccessToken token2 = new AccessToken(StaticConstants.TOKEN_KEY, StaticConstants.TOKEN_SECRET);
        twitterStream2.setOAuthAccessToken(token2);

        twitterStream2.addListener(statusListner);
        FilterQuery query = new FilterQuery();
        query.follow(new long[]{12384567l});
        twitterStream2.filter(query);

此处,我们必须收听其他人(不一定是粉丝)转发的推文的用户的用户ID将作为query.follow()中的参数传递。 这对我来说非常好,并且是推荐的方式。它绝不是一个黑客。我相信它会帮助那些在网上搜索类似主题的人,并会在SO上回答许多类似的未回答的问题。