我使用以下代码发布到Twitter。但我在logcat中收到11-06 17:11:28.811: D/Twitter Update Error(22688): No authentication challenges found
错误,并且状态未发布。相同的代码适用于其他应用程序。
更新
在我的应用中,任意数量的用户都可以通过Twitter注册。成功登录后,我曾经为特定用户存储db中的令牌。如果不同的用户喜欢通过Twitter分享,我会重置accessstoken,并允许登录对话框出现,并存储该用户令牌。如果特定用户的数据库中存在令牌,我直接从db获取令牌并使用它。
try {
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.setOAuthConsumerKey(TWITTER_CONSUMER_KEY);
builder.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET);
// Access Token
access_token = mSharedPreferences.getString(
PREF_KEY_OAUTH_TOKEN, "");
// Access Token Secret
access_token_secret = mSharedPreferences.getString(
PREF_KEY_OAUTH_SECRET, "");
AccessToken accessToken = new AccessToken(access_token,
access_token_secret);
Twitter twitter = new TwitterFactory(builder.build())
.getInstance(accessToken);
// Update status
twitter4j.Status response = twitter.updateStatus(status);
Log.d("Status", "> " + response.getText());
} catch (TwitterException e) {
// Error in updating status
Log.d("Twitter Update Error", e.getMessage());
}
答案 0 :(得分:0)
// try this
try {
// Access Token
access_token = mSharedPreferences.getString(
PREF_KEY_OAUTH_TOKEN, "");
// Access Token Secret
access_token_secret = mSharedPreferences.getString(
PREF_KEY_OAUTH_SECRET, "");
AccessToken accessToken = new AccessToken(access_token,
access_token_secret);
final Twitter twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer("twitterConsumerKey", "twitterSecreatKey");
twitter.setOAuthAccessToken(accessToken);
// Update status
twitter4j.Status response = twitter.updateStatus(status);
Log.d("Status", "> " + response.getText());
} catch (TwitterException e) {
// Error in updating status
Log.d("Twitter Update Error", e.getMessage());
}
答案 1 :(得分:0)
试试这个:
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.setOAuthAccessToken("ACCESS_TOKEN");
builder.setOAuthAccessTokenSecret("TOKEN_SECRET");
builder.setOAuthConsumerKey("CLIENT_ID");
builder.setOAuthConsumerSecret("CLIENT_SECRET");
OAuthAuthorization auth = new OAuthAuthorization(builder.build());
Twitter twitter = new TwitterFactory().getInstance(auth);
twitter.updateStatus("TEXT TO POST");