manuel
manuel

Reputation: 1197

Never Ending Story: Twitter Authentication

It seems to be a never ending story, but I cant get access to Twitter from Twitter4J stream API.

  1. I created a Twitter Account and changed the timezone to my home country
  2. I created an App in Twitter
  3. I created a ClientApp in Java/Maven with Twitter4J Streaming API 4.0.0
  4. I placed the Api-Key, Secret-Key and the Access-Tokens in the Configuration Builder
  5. I get an Exception from Twitter

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true);
    cb.setOAuthConsumerKey("xxxxxxxxxx");
    cb.setOAuthConsumerSecret("xxxxxxxxxxx");
    cb.setOAuthAccessToken("xxxxxxxxxxxxx");
    cb.setOAuthAccessTokenSecret("xxxxxxxxxxxx");
    
    TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance();
    
    StatusListnerImpl statusListenerImpl = new StatusListnerImpl();
    
    twitterStream.addListener(statusListenerImpl);
    
    twitterStream.sample();
    

401:Authentication credentials (https://dev.twitter.com/pages/auth) were missing or incorrect. Ensure that you have set valid consumer key/secret, access token/secret, and the system clock is in sync.

Please help.

Upvotes: 1

Views: 100

Answers (1)

pgaref
pgaref

Reputation: 51

For cleaness and simplicity I would recommend creating a separate file under project_name/resources/twitter4j.properties with contents:

debug=true
##Get the following from https://dev.twitter.com/
oauth.consumerKey=XXX
oauth.consumerSecret=XXX
oauth.accessToken=XXX
oauth.accessTokenSecret=XXX

Then you can create a new TwitterStream object as simple as:

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

Just make sure the properties file is available at compile time. Since you are using maven add the code below in pom.xml file (in build section):

        <resources>
            <resource>
                <directory>${basedir}/resources</directory>
            </resource>
        </resources>

PS: you might want to hide your application specific keys/tokens

Upvotes: 1

Related Questions