gpt4 book ai didi

Android Twitter 无法检索访问 token

转载 作者:太空宇宙 更新时间:2023-11-03 10:55:02 24 4
gpt4 key购买 nike

调用

provider.RetrieveAccesstoken(comsumer, verifier);

但奇怪的是我有时只会收到此错误。首先我的代码工作 en 我发布了一条消息。然后第二天我尝试在我的

上重新安装该应用程序

家用电脑出现这个错误。回到我的工作站,我又遇到了同样的错误,但在按下启动程序的按钮后,它又开始工作了,

在 Twitter 上发布了一条消息。这到底是怎么回事?!有人可以解释一下吗?

我在 android 2.2 上运行 2 个模拟器,它在 WQVGA400 上比在 WVGA800 模拟器上运行更频繁:S

我还收到通知“BufferedReader 构造函数中使用的默认缓冲区大小。如果需要 8k 字符的缓冲区,最好是显式的。”

这是否与我的内存或存储 token secret 有关?联系它时,听起来该服务太忙而无法回复。

代码

为了便于阅读,我删除了异常和导入

公共(public)类 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) {
}

最佳答案

成功了!而不是使用 Oath 路标库

provider.retrieveAccessToken(消费者,验证者)

我使用了 twitter4j 库

getOAuthAccessToken(requestToken, 验证者)

    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();
}

关于Android Twitter 无法检索访问 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6617352/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com