Twitter的回调并登录使用Jtwitter [英] Twitter Callback and Login Using Jtwitter
问题描述
public class TwitterActivity extends Activity
{
private Twitter twitter;
private OAuthProvider provider;
private CommonsHttpOAuthConsumer consumer;
String CONSUMER_KEY = "abcdefgh";
String CONSUMER_SECRET = "abcdefgh";
String CALLBACK = "twitterapp://connect";
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
askOAuth();
}
private void askOAuth() {
try {
consumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
provider = new DefaultOAuthProvider("http://twitter.com/oauth/request_token",
"http://twitter.com/oauth/access_token",
"http://twitter.com/oauth/authorize");
String authUrl = provider.retrieveRequestToken(consumer, CALLBACK);
Toast.makeText(this, "Please authorize this app!", Toast.LENGTH_LONG).show();
this.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl)));
} catch (Exception e) {
Log.e(APP, e.getMessage());
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Uri uri = intent.getData();
if (uri != null && uri.toString().startsWith(CALLBACK)) {
String verifier = uri.getQueryParameter(oauth.signpost.OAuth.OAUTH_VERIFIER);
try {
// this will populate token and token_secret in consumer
provider.retrieveAccessToken(consumer, verifier);
// TODO: you might want to store token and token_secret in you app settings!!!!!!!!
AccessToken a = new AccessToken(consumer.getToken(), consumer.getTokenSecret());
// initialize Twitter4J
twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
twitter.setOAuthAccessToken(a);
// create a tweet
Date d = new Date(System.currentTimeMillis());
String tweet = "#OAuth working! " + d.toLocaleString();
// send the tweet
twitter.updateStatus(tweet);
} catch (Exception e) {
Log.e(APP, e.getMessage());
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
}
P.S.: I have used these api's
[1]: https://github.com/punitmg/Twitter-Test-App
[2]: https://github.com/grantland/twitter-android-sdk
[3]: https://github.com/yusuke/twitter4j/
通过使用这些API的,我能够成功地鸣叫。但不幸的是下面提到的2屏幕出现在所有的上面3个..
By using all these API's i am able to tweet successfully. But unfortunately the below mentioned 2 screens are appearing in all the above 3 cases..
我要的是完成()或关闭低于两倍屏幕的完成我的鸣叫的时候。
All i want is to finish() or close the below two screen when my tweet is completed.
So .... plz guide me if i was wrong ...
推荐答案
您需要强制登录第二次来解决这个问题,
You need to force login second time to resolve this issue,
因此,只要添加 force_login = TRUE 在你的Twitter授权的网址。
So just add the force_login=true in your twitter authorize url.
我的意思只是改变了下面这一行code在 TwitterApp.java 类(如果你有不同名称的类文件,然后在你的项目是 DefaultOAuthProvider )
I mean just change the below line of code in TwitterApp.java class (if you have a class file with different name then search in your project with DefaultOAuthProvider)
mHttpOauthprovider = new DefaultOAuthProvider("http://twitter.com/oauth/request_token",
"http://twitter.com/oauth/access_token",
"http://twitter.com/oauth/authorize?force_login=true");
添加后的 force_login = TRUE 的WebView加载问题会解决,但每次需要输入密码的时间。
after adding force_login=true webview loading issue will be resolve but every time you need to enter login and password.
这篇关于Twitter的回调并登录使用Jtwitter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!