我想在我的
Android应用程序中使用Twitter4j发推文.这是我的代码:
//TWITTER SHARE. @Click (R.id. img_btn_twitter) @Background public void twitterPostWall(){ try { //Twitter Conf. ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true) .setOAuthConsumerKey(CONSUMER_KEY) .setOAuthConsumerSecret(CONSUMER_SECRET) .setOAuthAccessToken(ACCESS_KEY) .setOAuthAccessTokenSecret(ACCESS_SECRET); TwitterFactory tf = new TwitterFactory(cb.build()); Twitter twitter = new TwitterFactory().getInstance(); twitter.setOAuthConsumer(CONSUMER_KEY,CONSUMER_SECRET); try { RequestToken requestToken = twitter.getOAuthRequestToken(); Log.e("Request token: ","" + requestToken.getToken()); Log.e("Request token secret: ","" + requestToken.getTokenSecret()); AccessToken accessToken = null; } catch (IllegalStateException ie) { if (!twitter.getAuthorization().isEnabled()) { Log.e("OAuth consumer key/secret is not set.",""); } } Status status = twitter.updateStatus(postLink); Log.e("Successfully updated the status to [","" + status.getText() + "]."); } catch (TwitterException te) { Log.e("TWEET Failed",""); } }
我总是从Twitter4j收到此错误消息:java.lang.IllegalStateException:缺少身份验证凭据.有关详细信息,请参阅http://twitter4j.org/en/configuration.html.但是你可以看到我正在使用构建器设置我的密钥.有人可以帮我解决一下吗?谢谢.
解决方法
问题是以下几行.
TwitterFactory tf = new TwitterFactory(cb.build()); Twitter twitter = new TwitterFactory().getInstance();
您正在将配置传递给一个TwitterFactory实例,并使用另一个TwitterFactory实例来获取Twitter实例.
因此,你得到了
java.lang.IllegalStateException:缺少身份验证凭据
//Twitter Conf. ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true) .setOAuthConsumerKey(CONSUMER_KEY) .setOAuthConsumerSecret(CONSUMER_SECRET) .setOAuthAccessToken(ACCESS_KEY) .setOAuthAccessTokenSecret(ACCESS_SECRET); TwitterFactory tf = new TwitterFactory(cb.build()); Twitter twitter = tf.getInstance();
并使用此twitter实例.它会工作.