Android Twitter4j - TwitterException(未发现身份验证问题。)

时间:2013-12-05 15:00:43

标签: android api authentication twitter integration

我正在制作一个应用程序,它只能从android中的特定页面获取推文。我使用的是twitter4j库3.0.5版。以下是我的代码:

    @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;
    }
  

(上述代码是在doInBackground的{​​{1}}方法中完成的。只是觉得你应该知道)

我收到错误消息:AsyncTask

我查看了以前的帖子,并说要检查我的日期和时间。我做到了,他们都是正确的,并设置为自动。 (因为我有一个慢的互联网连接)我也尝试在更快的互联网连接上运行它。我现在认为我的代码有问题。

我不熟悉将twitter整合到我的Android应用程序中,因此解释性指导也会对答案有所帮助。

...谢谢

1 个答案:

答案 0 :(得分:0)

Twitter4j适应单线态并保持其登录/身份验证状态,并在每次尝试登录/验证时引发异常

您需要做的是将身份验证登录与其余API调用分开。在你的情况下,你需要这样的东西:

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;

}

确保从后台线程调用这两个方法,并在每次运行时调用login。