java - 我是否正确地向 Twitter 进行了身份验证以及阅读 1 小时英文推文的建议

标签 java twitter oauth twitter4j

大家好,我是 Twitter 新手,刚从 Python 回到 Java。

我正在尝试使用 oauth 通过 twitter4j 实时验证和抓取推文。

所以当我运行下面的代码 list 时,它看起来不错,但我不确定我是否正在连接!

此外,我不确定如何收听流和提取推文。

到目前为止我的代码:

package twitterfordatalake;

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.Properties;

    import twitter4j.StallWarning;
    import twitter4j.Status;
    import twitter4j.StatusDeletionNotice;
    import twitter4j.StatusListener;
    import twitter4j.TwitterException;
    import twitter4j.TwitterStream;
    import twitter4j.TwitterStreamFactory;
    import twitter4j.conf.ConfigurationBuilder;
    import static twitterfordatalake.TwitterForDataLake.props;
    //Testing
    import twitter4j.TwitterFactory; 
    import twitter4j.Twitter; 
    import twitter4j.Query;
    import twitter4j.QueryResult;

    /**
     *
     * @author crigano
     */
    public class TwitterForDataLake 
    {
        static Properties props = new Properties();

        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) throws TwitterException, IOException
        {
              // TODO code application logic here
              System.out.println("Hellow World");

              TwitterForDataLake ts = new TwitterForDataLake();
              ts.TwitterScraper();

         }

        public void TwitterScraper() throws IOException
        {
                // read the properties file options
                ConfigurationBuilder cb = new ConfigurationBuilder();
                cb.setDebugEnabled(true)
                .setOAuthConsumerKey(
                    props.getProperty("oauth.consumerKey"))
                .setOAuthConsumerSecret(
                    props.getProperty("oauth.consumerSecret"))
                .setOAuthAccessToken(
                    props.getProperty("oauth.accessToken"))
                .setOAuthAccessTokenSecret(
                    props.getProperty("oauth.accessTokenSecret"));

               TwitterStream twitterStream = new TwitterStreamFactory(cb.build())
                    .getInstance(); 

           }

    }

I used the following initialization twitter4j.properties file:
debug=true
oauth.consumerKey=AAAA
oauth.consumerSecret=BBBB
oauth.accessToken=CCCC
oauth.accessTokenSecret=DDDD

This is based on what I got from twitter:
AAAA
API secret BBBB
Access level Read, write, and direct messages (modify app permissions)
Owner me
Owner ID 66666 

Your access token
This access token can be used to make API requests on your own account's behalf. Do not share your access token secret with anyone.
Access token CCCC
Access token secret DDDD
Access level Read, write, and direct messages
Owner me
Owner ID 6666

我非常感谢有关调试誓言的一些建议。

我非常感谢一个小时的英文推文监听器的任何示例代码。

非常感谢,

克里斯

最佳答案

我在 java 中粘贴了我的运行代码。您可以将它用于相同的目的:

//首先我们需要一个带有有效授权 token 的 twitter 对象。

public static ConfigurationBuilder confBuilder = null;
public static Configuration config = null;
public static Twitter twitterObj = null;

confBuilder = new twitter4j.conf.ConfigurationBuilder();
confBuilder.setDebugEnabled(true);

confBuilder.setOAuthConsumerKey("consumerKey");
confBuilder.setOAuthConsumerSecret("consumerSecret");
confBuilder.setOAuthAccessToken("accessToken");
confBuilder.setOAuthAccessTokenSecret("accessTokenSecret");

config = confBuilder.build();

twitter4j.TwitterFactory tf = new twitter4j.TwitterFactory(config);
twitterObj = tf.getInstance(); //Here, finally we got the twitter object.

//后面要用到streaming函数也是这样:

TwitterStream twitterStream = new TwitterStreamFactory(config).getInstance();

        StatusListener statusListener = new StatusListener() {
            private int count = 0;
            private long originalTweetId = 0;

            @Override
            public void onStatus(Status status) {

            // status object here is the tweet object that caught by the stream. You    can do whatever you want with it.

            }

            @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.");
            }

            @Override
            public void onException(Exception ex) {
                logWriter.WriteErrorLog(ex, "onException()");
            }
        };

        FilterQuery fq = new FilterQuery();

        String keywords[] = {keyword};

        fq.track(keywords);

        twitterStream.addListener(statusListener);
        twitterStream.filter(fq);

//我希望这就是您要找的。祝你好运!

关于java - 我是否正确地向 Twitter 进行了身份验证以及阅读 1 小时英文推文的建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22119881/

相关文章:

android - 使用路标android的twitter身份验证

hadoop - Storm Twitter 访问 key 不正确或丢失

java - Spring OAuth2 为每个请求生成访问 token 到 token 端点

java - 是否有一个包可以实现非常大的高精度数字?

java - 在 Volley 请求中将图像作为带有其他参数的多部分发送

java - 如何映射具有双向关系的三元日期映射?

facebook - 如何在我的网站上管理来自多提供商(facebook、twitter)的用户帐户?

node.js - OAuth 2.0 Flow 它是如何工作的 node-oauth2-server

javascript - Strongloop Passport.js 身份验证 - "Failed to obtain access token"

java - 在不更改原始方法签名的情况下对服务方法进行分页调用