我收到错误“在服务器回复中未设置请求令牌或令牌密钥。您使用的服务提供商可能有错误。”在调用
时provider.RetrieveAccesstoken(comsumer,verifier);
但奇怪的是我有时只会得到这个错误。首先我的代码工作,我发布了一条消息。然后第二天我尝试在我的
上重新安装应用程序家用电脑我遇到了这个错误。回到我的工作站后,我又遇到了同样的错误,但在按下启动程序的按钮后再次工作并
在Twitter上发布了一条消息。这到底是怎么回事?!有人可以解释一下吗?
我在Android 2.2上运行2个模拟器,它在WQVGA400上更常用,然后在WVGA800模拟器上运行:S
我还得到通知“BufferedReader构造函数中使用的默认缓冲区大小。如果需要8k-char缓冲区,最好是显式的。”
这是否必须对我的记忆或存储令牌秘密做些什么?听起来服务太忙了,无法在联系时回复。
CODE
为了便于阅读,我删除了例外和导入
公共类TestAgainActivity扩展了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) {
}
答案 0 :(得分:1)
有效! 而不是使用誓言路标库
provider.retrieveAccessToken(消费者,验证者)
我使用了twitter4j库
getOAuthAccessToken(requestToken,verifier)
String verifier = uri.getQueryParameter(OAuth.OAUTH_VERIFIER);
Twitter twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
AccessToken accessToken = null;//get stored request tokens from database
mDbHelper = DbAdapter.getInstance();
mDbHelper.open();
String[] dbRequestTokens = mDbHelper.getTwitterRequestTokens();
mDbHelper.close();
RequestToken requestToken = new RequestToken(dbRequestTokens[0], dbRequestTokens[1]);
try {
accessToken = twitter.getOAuthAccessToken(requestToken, verifier);
//dataBase store accessTokens to tweet message or acces other twitter recources, and don't forget to set the OAuth Consumer with consumer key and secret
mDbHelper.open();
mDbHelper.setTwitterAccessTokens(accessToken.getToken(), accessToken.getTokenSecret());
mDbHelper.close();
return "logged in succesfully";
} catch (TwitterException e) {
e.printStackTrace();
return e.getMessage();
}