当调用provider.RetrieveAccesstoken(comsumer, verifier)时,我会收到错误消息“服务器回复中未设置请求令牌或令牌密钥。您使用的服务提供商可能存在问题。”但奇怪的是,我只有在某些情况下才会出现此错误。起初我的代码可以正常工作并发布消息。然后第二天,当我尝试在家里的电脑上重新安装应用程序时,我遇到了这个错误。回到我的工作站后,我再次遇到了相同的错误,但在按下启动过程的按钮后,它又能够正常工作并在Twitter上发布消息。这到底是怎么回事?有人能解释一下吗?
我在Android 2.2上运行了两个模拟器,它在WQVGA400模拟器上的工作频率比WVGA800模拟器上的工作频率更高。
我还收到通知“BufferedReader构造函数中使用了默认缓冲区大小。如果需要8k字符缓冲区,则最好明确指定。”这是否与我的内存或存储令牌密钥有关?听起来像是服务繁忙无法在联系时进行回复。
代码:
为了易读性,我删除了异常和导入
public class TestAgainActivity extends Activity {
private String CONSUMER_KEY = "xxxxx";
private String CONSUMER_SECRET = "xxxxxxxxxxxx";
private String CALLBACK_URL = "myapp://twit";
private OAuthConsumer consumer;
private OAuthProvider provider;
private String authUrl;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button loginButton = new Button(this);
loginButton = (Button)findViewById(R.id.button1);
loginButton.setOnClickListener(login);
consumer = new CommonsHttpOAuthConsumer(
CONSUMER_KEY, CONSUMER_SECRET);
provider = new DefaultOAuthProvider(
"https://api.twitter.com/oauth/request_token",
"https://api.twitter.com/oauth/access_token",
"https://api.twitter.com/oauth/authorize");
}
private OnClickListener login = new OnClickListener() {
public void onClick(View v) {
AskOauth();
}
};
public void AskOauth(){
try {
authUrl = provider.retrieveRequestToken(consumer, CALLBACK_URL);
Here i store the Consumer token and tokenSecret
SharedPreferences savedTokens = getSharedPreferences("savedTokens", 0);
SharedPreferences.Editor editor = savedTokens.edit();
editor.putString("savedToken", consumer.getToken());
editor.putString("savedTokenSecret", consumer.getTokenSecret());
editor.commit();
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl)));
} catches{
}
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
SharedPreferences savedTokens = getSharedPreferences("savedTokens", 0);
String saved_token = savedTokens.getString("savedToken", "empty ShP");
String saved_tokenSecret = savedTokens.getString("savedTokenSecret", "empty ShP");
consumer.setTokenWithSecret(saved_token, saved_tokenSecret);
System.out.println("consumer token "+consumer.getToken());
System.out.println("consumer tokenSecret "+consumer.getTokenSecret());
Uri uri = this.getIntent().getData();
if (uri != null && uri.toString().startsWith(CALLBACK_URL)) {
SharedPreferences savedTokens = getSharedPreferences("savedTokens", 0);
String saved_token = savedTokens.getString("savedToken", "empty ShP");
String saved_tokenSecret = savedTokens.getString("savedTokenSecret", "empty ShP");
consumer.setTokenWithSecret(saved_token, saved_tokenSecret);
String verifier = uri.getQueryParameter(OAuth.OAUTH_VERIFIER);
try {
Here i sometime get the error!
provider.retrieveAccessToken(consumer, verifier);
AccessToken accesToken = new AccessToken(consumer2.getToken(), consumer2.getTokenSecret());
Twitter twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
twitter.setOAuthAccessToken(accesToken);
Date d = new Date(System.currentTimeMillis());
String tweet = "This message is tweeted " + d.toLocaleString();
try {
twitter.updateStatus(tweet);
} catch (TwitterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (OAuthMessageSignerException e1) {
}