Naveen,
The solution is very simple, you create a Twitterutil class and use it for
sending Tweets and also getting the Tweets. Below is my example to do it.
public class TwitterUtils {
public static boolean isAuthenticated(SharedPreferences prefs) {
String token = prefs.getString(OAuth.OAUTH_TOKEN, "");
String secret = prefs.getString(OAuth.OAUTH_TOKEN_SECRET, "");
AccessToken a = new AccessToken(token, secret);
Twitter twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(AppConstants.CONSUMER_KEY,
AppConstants.CONSUMER_SECRET);
twitter.setOAuthAccessToken(a);
try {
twitter.getAccountSettings();
return true;
} catch (TwitterException e) {
return false;
}
}
public static void sendTweet(SharedPreferences prefs, String msg)
throws Exception {
String token = prefs.getString(OAuth.OAUTH_TOKEN, "");
String secret = prefs.getString(OAuth.OAUTH_TOKEN_SECRET, "");
AccessToken a = new AccessToken(token, secret);
Twitter twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(AppConstants.CONSUMER_KEY,
AppConstants.CONSUMER_SECRET);
twitter.setOAuthAccessToken(a);
twitter.updateStatus(msg);
}
public static String getTweets(SharedPreferences prefs) {
String token = prefs.getString(OAuth.OAUTH_TOKEN, "");
String secret = prefs.getString(OAuth.OAUTH_TOKEN_SECRET, "");
StringBuffer stb = new StringBuffer();
AccessToken a = new AccessToken(token, secret);
Twitter twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(AppConstants.CONSUMER_KEY,
AppConstants.CONSUMER_SECRET);
twitter.setOAuthAccessToken(a);
try {
ResponseList<Status> liststatus = twitter.getFriendsTimeline();
for (Status status : liststatus) {
stb.append(status.getText());
// System.out.println("I am reading other Twitter Status " +
// status.getText());
}
} catch (TwitterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return stb.toString();
}
To use it..
if (TwitterUtils.isAuthenticated(prefs)) { //if user logged in , send the
twit
sendTweet(); //
}
public void sendTweet() {
Thread t = new Thread() {
public void run() {
try {
TwitterUtils.sendTweet(prefs,getTweetMsg());
mTwitterHandler.post(mUpdateTwitterNotification);
} catch (Exception ex) {
ex.printStackTrace();
}
}
};
t.start();
}
--
Warm Regards,
*Mukesh Kumar*,
Android Consultant/Freelancer,
India,Hyderabad.
On Fri, Jan 6, 2012 at 11:57 AM, chowdary nani <[email protected]>wrote:
> Hi All,
> I am working on twittwer in tegration in android
> I am trying to post tweet on my twitter but i am getting error
> as " Error during OAUth retrieve request token"
> here is the followin code:
>
>
> tweet.setOnClickListener(new View.OnClickListener() {
> public void onClick(View v) {
> if (TwitterUtils.isAuthenticated(prefs)) {
> sendTweet();
> } else {
>
> Intent i = new Intent(getApplicationContext(),
> PrepareRequestTokenActivity.class);
> i.putExtra("tweet_msg", getTweetMsg());
> startActivity(i);
> }
> }
> });
>
>
> public void sendTweet() {
> Thread t = new Thread() {
> public void run() {
>
> try {
> TwitterUtils.sendTweet(prefs, getTweetMsg());
> mTwitterHandler.post(mUpdateTwitterNotification);
> } catch (Exception ex) {
> ex.printStackTrace();
> }
> }
>
> };
> t.start();
> }
> public class PrepareRequestTokenActivity extends Activity {
>
> final String TAG = getClass().getName();
>
> private OAuthConsumer consumer;
> private OAuthProvider provider;
>
> @Override
> public void onCreate(Bundle savedInstanceState) {
> super.onCreate(savedInstanceState);
> try {
> this.consumer = new CommonsHttpOAuthConsumer(
> Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET);
> this.provider = new CommonsHttpOAuthProvider(Constants.REQUEST_URL,
> Constants.ACCESS_URL, Constants.AUTHORIZE_URL);
> } catch (Exception e) {
> Log.e(TAG, "Error creating consumer / provider", e);
> }
>
> Log.i(TAG, "Starting task to retrieve request token.");
> new OAuthRequestTokenTask(this, consumer, provider).execute();
> }
>
> @Override
> public void onNewIntent(Intent intent) {
> super.onNewIntent(intent);
> SharedPreferences prefs = PreferenceManager
> .getDefaultSharedPreferences(this);
> final Uri uri = intent.getData();
> if (uri != null
> && uri.getScheme().equals(Constants.OAUTH_CALLBACK_SCHEME)) {
> Log.i(TAG, "Callback received : " + uri);
> Log.i(TAG, "Retrieving Access Token");
> new RetrieveAccessTokenTask(this, consumer, provider, prefs)
> .execute(uri);
> finish();
> }
> }
>
> public class RetrieveAccessTokenTask extends AsyncTask<Uri, Void, Void> {
>
> private Context context;
> private OAuthProvider provider;
> private OAuthConsumer consumer;
> private SharedPreferences prefs;
>
> public RetrieveAccessTokenTask(Context context, OAuthConsumer consumer,
> OAuthProvider provider, SharedPreferences prefs) {
> this.context = context;
> this.consumer = consumer;
> this.provider = provider;
> this.prefs = prefs;
> }
>
> /**
> * Retrieve the oauth_verifier, and store the oauth and
> * oauth_token_secret for future API calls.
> */
> @Override
> protected Void doInBackground(Uri... params) {
> final Uri uri = params[0];
> final String oauth_verifier = uri
> .getQueryParameter(OAuth.OAUTH_VERIFIER);
>
> try {
> provider.retrieveAccessToken(consumer, oauth_verifier);
>
> final Editor edit = prefs.edit();
> edit.putString(OAuth.OAUTH_TOKEN, consumer.getToken());
> edit.putString(OAuth.OAUTH_TOKEN_SECRET,
> consumer.getTokenSecret());
> edit.commit();
>
> String token = prefs.getString(OAuth.OAUTH_TOKEN, "");
> String secret = prefs.getString(OAuth.OAUTH_TOKEN_SECRET, "");
>
> consumer.setTokenWithSecret(token, secret);
> context.startActivity(new Intent(context,
> AndroidTwitterSample.class));
>
> executeAfterAccessTokenRetrieval();
>
> Log.i(TAG, "OAuth - Access Token Retrieved");
>
> } catch (Exception e) {
> Log.e(TAG, "OAuth - Access Token Retrieval Error", e);
> }
>
> return null;
> }
>
> private void executeAfterAccessTokenRetrieval() {
> String msg = getIntent().getExtras().getString("tweet_msg");
> try {
> TwitterUtils.sendTweet(prefs, msg);
> } catch (Exception e) {
> Log.e(TAG, "OAuth - Error sending to Twitter", e);
> }
> }
> }
>
> }
>
>
>
> please any one help me in solving this problem
>
>
>
>
> Thanks
> Naveen.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en