我正在尝试使用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();
}
}


错误:运行此代码时,它给我“找不到身份验证挑战”错误。
 我试图用前面的问题解决这个问题,但是失败了。

java - twitter4j“未发现身份验证挑战”-LMLPHP

link to same image(if you don't find it clear here)

有人可以告诉我我在这里犯什么错误吗?

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

最佳答案

请尝试以下方式:

   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);

07-25 21:27