我正在研究一个非常简单的Android twitter应用程序,但是当我尝试在Twitter上发布推文时,它给了我收到的身份验证是null错误,并且在Android 4.1中它强制关闭应用程序我在网上搜索但没有找到任何帮助
enter public class StatusClass extends Activity implements OnClickListener{
private Button update;
Twitter twitter;
ConfigurationBuilder cb;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.update_status);
update = (Button)findViewById(R.id.bUpdate);
update.setOnClickListener(this);
cb = new ConfigurationBuilder();
cb.setOAuthConsumerKey("My_Consumer_key");
cb.setOAuthConsumerSecret("My_Consumer_secret_key");
cb.setOAuthAccessToken("My_Oath_access_token");
cb.setOAuthAccessTokenSecret("My_Token_secret");
twitter = new TwitterFactory(cb.build()).getInstance();
}
@Override
public void onClick(View v) {
String tweet="HELLO WORLD!";
try {
Status status = twitter.updateStatus(tweet);
} catch (TwitterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
W/System.err( 1798): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.processAuthHeader(HttpURLConnectionImpl.java:1153)
答案 0 :(得分:1)
您是否尝试过以下代码?:
AccessToken accessToken = new AccessToken(ACCESS_TOKEN, ACCESS_TOKEN_SECRET);
OAuthAuthorization authorization = new OAuthAuthorization(ConfigurationContext.getInstance(), CONSUMER_KEY, CONSUMER_SECRET, accessToken);
Twitter twitter = new TwitterFactory().getInstance(authorization);
同时尝试基本授权:
Configuration configuration = new ConfigurationBuilder()
.setOAuthConsumerKey("your_customer_key")
.setOAuthConsumerSecret("your_customer_secret")
.build();
Twitter twitter = new TwitterFactory(configuration).getInstance(new BasicAuthorization("username", "password"));
AccessToken token = twitter.getOAuthAccessToken();