使用twitter4j“没有发现身份验证挑战”

时间:2016-06-06 11:41:13

标签: java twitter twitter4j

我正在尝试使用twitter4j与Twitter签名。 我的代码是

public class MainActivity extends AppCompatActivity {
    private static Twitter twitter;
    private static RequestToken requestToken;
    static String consumerKey = "my_consumer_key";
    static String consumerSecretKey = "my_consumer_secret_key";
    static String accessToken = "my_access_token";
    static String accessSecretToken = "my_access__secret_token";
    User user;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

   final ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true)
            .setOAuthConsumerKey(consumerKey)
            .setOAuthConsumerSecret(consumerSecretKey)
            .setOAuthAccessToken(accessToken)
            .setOAuthAccessTokenSecret(accessSecretToken);
    Thread thread = new Thread(new Runnable()
    {
        @Override
        public void run() {
            try {
                TwitterFactory factory = new TwitterFactory(cb.build());
                Twitter twitter = factory.getInstance();

                System.out.println(twitter.getScreenName());
                     //getting error because of this line

            } catch (TwitterException te) {
                te.printStackTrace();
            }
        }
    });

    thread.start();
}
}

错误:当我运行此代码时,它会给我“未发现身份验证挑战”错误。
我试图用之前提出的问题解决这个问题但是失败了。

enter image description here

link to same image(if you don't find it clear here)
谁能告诉我我在这里做错了什么?

注意:如果我将.setApplicationOnlyAuthEnabled(true)添加到cb,则会收到新错误既未提供用户ID /密码也未提供OAuth使用方密钥/密钥组合。< /强>

1 个答案:

答案 0 :(得分:0)

尝试以下方式:

   final ConfigurationBuilder cb = new ConfigurationBuilder();
        .setOAuthConsumerKey(consumerKey)
        .setOAuthConsumerSecret(consumerSecretKey)
        .setOAuthAccessToken(accessToken)
        .setOAuthAccessTokenSecret(accessSecretToken);
        OAuthAuthorization auth = new OAuthAuthorization(cb.build());
        Twitter twitter = new TwitterFactory().getInstance(auth);