Prerequisites:
- Consumer Key
- Consumer key secret
- Signpost-core and signpost-commonshttp libs
Approach:
launch the twitter authentication in a webview and present the login page to user in it.
When user enters the credentials, validate them and proceed.
Activity TwitterConnectWebActivity:
** You can copy the following whole activity in your code, as its a standalone code
public class TwitterConnectWebActivity extends Activity {
private static final String TAG = "TwitterConnectWebActivity";
private static String TWITTER_IMAGE_URL = "http://api.twitter.com/1/users/profile_image/@user.json";
private static final int LOADING_DIALOG_ID = 0;
protected final String CALLBACKURL = "OauthTwitter://taylorswiftapp";
protected static CommonsHttpOAuthConsumer twitterHttpOauthConsumer;
protected static OAuthProvider twitterHttpOauthprovider;
protected int TWITTER_LOGIN_REQUEST_CODE = 1;
protected int TWITTER_LOGOUT_REQUEST_CODE = 2;
AccessToken twitterAuthToken;
Context context;
WebView webview;
ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tw_connect_web_activity);
context = this;
webview = (WebView) findViewById(R.id.webview);
progressBar = (ProgressBar) findViewById(R.id.progress_bar);
new RequestTokenSecretUrlTask().execute();
}
private class RequestTokenSecretUrlTask extends
AsyncTask<Void, Void, String> {
@Override
protected void onPreExecute() {
showDialog(LOADING_DIALOG_ID);
}
@Override
protected String doInBackground(Void... params) {
try {
twitterHttpOauthConsumer = new CommonsHttpOAuthConsumer( "YOUR_CONSUMER_KEY",
"YOUR_CONSUMER_SECRET");
twitterHttpOauthprovider = new DefaultOAuthProvider(
"https://api.twitter.com/oauth/request_token",
"https://api.twitter.com/oauth/access_token",
"https://api.twitter.com/oauth/authorize");
String authUrl = twitterHttpOauthprovider.retrieveRequestToken(
twitterHttpOauthConsumer, CALLBACKURL);
Log.e(TAG, "Load URL : " + authUrl);
return authUrl;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String authUrl) {
dismissDialog(LOADING_DIALOG_ID);
webview.setWebViewClient(new TwitterWebViewClient());
webview.getSettings().setPluginsEnabled(true);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setLayoutAlgorithm(
WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
webview.loadUrl(authUrl);
}
}
private class TwitterWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.e(TAG, "shouldOverrideUrlLoading() -> url:" + url);
progressBar.setVisibility(View.VISIBLE);
Uri uri = Uri.parse(url);
if (uri != null && uri.toString().startsWith(CALLBACKURL)) {
String verifier = uri
.getQueryParameter(oauth.signpost.OAuth.OAUTH_VERIFIER);
try {
twitterHttpOauthprovider.retrieveAccessToken(
twitterHttpOauthConsumer, verifier);
twitterAuthToken = new AccessToken(twitterHttpOauthConsumer
.getToken(), twitterHttpOauthConsumer
.getTokenSecret());
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true).setOAuthConsumerKey(
twitterHttpOauthConsumer.getConsumerKey())
.setOAuthConsumerSecret(
twitterHttpOauthConsumer
.getConsumerSecret())
.setOAuthAccessToken(twitterAuthToken.getToken())
.setOAuthAccessTokenSecret(
twitterAuthToken.getTokenSecret());
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();
// if authentication is done,set result to ok finish this
// activity
Intent intent = new Intent();
intent
.putExtra("access_token", twitterAuthToken
.getToken());
intent.putExtra("access_secret", twitterAuthToken
.getTokenSecret());
String name = twitter.getScreenName();
intent.putExtra("twitter_screen_name", name);
intent.putExtra("twitter_uid", "" + twitter.getId());
intent
.putExtra("twitter_image_url", ""
+ twitter.getProfileImage(name,
ProfileImage.BIGGER));
setResult(RESULT_OK, intent);
finish();
} catch (Exception e) {
e.printStackTrace();
}
return true;
} else {
Log.e(TAG, " ::2222:: Twitter URl --- " + url);
view.loadUrl(url);
return true;
}
}
@Override
public void onPageFinished(WebView view, String url) {
progressBar.setVisibility(View.INVISIBLE);
}
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case LOADING_DIALOG_ID:
ProgressDialog dialog = new ProgressDialog(this);
dialog.setMessage("Loading...");
dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
public void onCancel(DialogInterface arg0) {
finish();
}
});
return dialog;
default:
return super.onCreateDialog(id);
}
}
}
NOW, from the point where you wanna start this twitter stuff, just call:
startActivity(new Intent(this, TwitterConnectWebActivity.class));
In manifest add this permission:
<uses-permission
android:name="android.permission.INTERNET" />
You are done.