Java tutorial
/* Copyright (c) 2013, Panteha Saeedi All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package com.ahuralab.mozaic.auth; import com.ahuralab.mozaic.db.TimelineDataSource; import oauth.signpost.OAuthProvider; import oauth.signpost.basic.DefaultOAuthProvider; import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer; import twitter4j.Twitter; import twitter4j.TwitterFactory; import twitter4j.conf.ConfigurationBuilder; import android.content.Context; import android.content.SharedPreferences; import android.net.Uri; import android.util.Log; /** * @author pani * */ public class TwitterAuthenticator { private static final String TWITTER_CREDENTIAL = "twitter_credential"; // TODO change this callback in sth that the browser will not handle public static final String CALLBACKURL = "http://ahuralab.com/mozaic";//;"twitterapp://connect"; public static final String consumerKey = "<add the consumerkey>"; public static final String consumerSecret = "<add the consumer Secret>"; private final OAuthProvider httpOauthprovider = new DefaultOAuthProvider( "https://api.twitter.com/oauth/request_token", "https://api.twitter.com/oauth/access_token", "https://api.twitter.com/oauth/authorize"); private final CommonsHttpOAuthConsumer httpOauthConsumer = new CommonsHttpOAuthConsumer(consumerKey, consumerSecret); public String getUrl(Context context) { try { return httpOauthprovider.retrieveRequestToken(httpOauthConsumer, CALLBACKURL); } catch (Exception e) { Log.w("oauth fail", e); //Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show(); return null; } } public void handleCallBack(Uri uri, Context context) { if (uri != null) { Log.d("", uri.toString()); try { final String oauthVerifier = uri.getQueryParameter("oauth_verifier"); httpOauthprovider.retrieveAccessToken(httpOauthConsumer, oauthVerifier); String userKey = httpOauthConsumer.getToken(); String userSecret = httpOauthConsumer.getTokenSecret(); // Save user_key and user_secret in user preferences and return SharedPreferences settings = context.getSharedPreferences(TWITTER_CREDENTIAL, 0); SharedPreferences.Editor editor = settings.edit(); editor.putString("user_key", userKey); editor.putString("user_secret", userSecret); editor.commit(); } catch (Exception e) { Log.e("", "" + e.getMessage(), e); } } else { // Do something if the callback comes from elsewhere //return; } } public Twitter createTwitter(Context context) { SharedPreferences settings = context.getSharedPreferences(TWITTER_CREDENTIAL, 0); ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true).setOAuthConsumerKey(consumerKey).setOAuthConsumerSecret(consumerSecret) .setOAuthAccessToken(settings.getString("user_key", null)) .setOAuthAccessTokenSecret(settings.getString("user_secret", null)); TwitterFactory tf = new TwitterFactory(cb.build()); Twitter twitter = tf.getInstance(); return twitter; } public boolean isAuthenticated(Context context) { SharedPreferences settings = context.getSharedPreferences(TWITTER_CREDENTIAL, 0); String key = settings.getString("user_key", null); String secret = settings.getString("user_secret", null); return key != null && secret != null; } public void twitterSignOut(Context context) { SharedPreferences settings = context.getSharedPreferences(TWITTER_CREDENTIAL, 0); SharedPreferences.Editor editor = settings.edit(); editor.remove(consumerKey); editor.remove(consumerSecret); editor.commit(); TimelineDataSource datasource = new TimelineDataSource(context); datasource.deleteTimeline(); } }