0

I am making an application which gets just tweets from a specific page in android. I am using the twitter4j library version 3.0.5. Here is my code below:

    @Override
    protected String doInBackground(String... credentials) {
        // TODO Auto-generated method stub
        oAuth_ConsumerKey = credentials[0];
        oAuth_ConsumerSecret = credentials[1];
        accessToken = credentials[2];
        accessTokenSecret = credentials[3];

        System.out.println("Credentials -------- " + oAuth_ConsumerKey
                + "\n" + oAuth_ConsumerSecret + "\n" + accessToken + "\n"
                + accessTokenSecret);

        ConfigurationBuilder builder = new ConfigurationBuilder();
        builder.setOAuthConsumerKey(oAuth_ConsumerKey)
        .setOAuthConsumerSecret(accessTokenSecret);
        builder.setOAuthAccessToken(accessToken);
        builder.setOAuthAccessTokenSecret(accessTokenSecret);

        TwitterFactory twitterFactory = new TwitterFactory(builder.build());
        Twitter twitter = twitterFactory.getInstance();

        try {
            List<twitter4j.Status> statuses;
            String user = "@pagename";

            statuses = twitter.getUserTimeline(user);
            for (int i = 0; i < statuses.size(); i++) {
                twitter4j.Status status = statuses.get(i);
                Log.i("Tweet Count " + (i + 1), status.getText() + "\n\n");
            }
        } catch (TwitterException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

(The above code is being done in the doInBackground method of the AsyncTask. Just thought you should know)

And I am getting an error saying: No authentication challenges found. Relevant discussions can be found on the Internet at: ...

I viewed previous posts and it said to check my date and time. I did that and they are all correct and set to automatic. (Since i have a slow internet connection) I also tried running it on a faster internet connection. I now think that there is something wrong with my code.

I am new to integrating twitter to my android app so explanatory guidance would also be helpful along with the answer.

Thanks...

Adifyr
  • 2,649
  • 7
  • 41
  • 62
  • possible duplicate of [Twitter 4j No authentication challenges found, Relevant discussions can be found at internet Exception](http://stackoverflow.com/questions/17103234/twitter-4j-no-authentication-challenges-found-relevant-discussions-can-be-found) – Mr.Me Dec 05 '13 at 15:03
  • well yes speaking in terms of the exception raised, this is a duplicate of numerous questions. What differs in my question is that I am not trying to post anything to a twitter page. I simply require that i get tweets. Second of all, I am new to twitter integration and like i stated above, I have tried the things that other posts have recommended. According to your link to the question, our code has different purposes and do different things. But thanks for the notice anyway. – Adifyr Dec 05 '13 at 15:10

1 Answers1

0

Twitter4j adapts singletone and it maintains its login/authentication status and raises exceptions everytime you try to login/authenticate

What you have to do is separate the authentication login from the rest of API calls. in your case you need something like this:

private Twitter mTwitter;
private void login(String[] credentials){
  String oAuth_ConsumerKey = credentials[0];
    String  oAuth_ConsumerSecret = credentials[1];
   String   accessToken = credentials[2];
   String   accessTokenSecret = credentials[3];

    System.out.println("Credentials -------- " + oAuth_ConsumerKey
            + "\n" + oAuth_ConsumerSecret + "\n" + accessToken + "\n"
            + accessTokenSecret);

    ConfigurationBuilder builder = new ConfigurationBuilder();
    builder.setOAuthConsumerKey(oAuth_ConsumerKey)
    .setOAuthConsumerSecret(accessTokenSecret);
    builder.setOAuthAccessToken(accessToken);
    builder.setOAuthAccessTokenSecret(accessTokenSecret);

    TwitterFactory twitterFactory = new TwitterFactory(builder.build());
    mTwitter = twitterFactory.getInstance();
}

 private List<Status> getTimeLine(){

   List<twitter4j.Status> statuses = null;
    try {

        String user = "@pagename";

        statuses = mTwitter.getUserTimeline(user);
        for (int i = 0; i < statuses.size(); i++) {
            twitter4j.Status status = statuses.get(i);
            Log.i("Tweet Count " + (i + 1), status.getText() + "\n\n");
        }

    } catch (TwitterException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
   return statuses;

}

Make sure you call both methods from background thread and call login once every run.

Mr.Me
  • 9,192
  • 5
  • 39
  • 51
  • I am getting an android lint error (Red line below code) in `getTimeline() { Twitter twitter = twitterFactory.getInstance();` saying that it cannot be resolved to a type. (Below twitterFactory.getInstance();) – Adifyr Dec 05 '13 at 15:46
  • sorry about that, you have to keep a reference for Twitter object instead of asking for it again, I've modified the code – Mr.Me Dec 05 '13 at 15:53
  • Hi, I am still getting the same error. Can you please show me how you do this in `doInBackground` in `AsyncTask`. – Adifyr Dec 05 '13 at 16:02