Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9143393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:00:49+00:00 2026-06-17T10:00:49+00:00

I’m trying to share some data using twitter in android app, so what ever

  • 0

I’m trying to share some data using twitter in android app, so what ever the basic information like redirecting url, application given in the “Twitter app registration page”, everything works fine but after giving the username and password in the LOGIN PAGE of twitter it doesn’t redirects to the next page, instead getting “this page contains too many server redirects” error message.

My redirect url looks like this "https://www.example.com/".

Any suggestions?

public class constants {
public static final String CONSUMER_KEY = "key";
public static final String CONSUMER_SECRET= "secret";

public static final String REQUEST_URL = "http://api.twitter.com/oauth/request_token";
public static final String ACCESS_URL = "http://api.twitter.com/oauth/access_token";
public static final String AUTHORIZE_URL = "http://api.twitter.com/oauth/authorize";


public static final String OAUTH_CALLBACK_URL = "x-latify-oauth-twitter";
private static final String CALLBACK_SCHEME = null;
public static final Object OAUTH_CALLBACK_SCHEME = CALLBACK_SCHEME + "://callback";

}

MainActivity

import java.util.Date;

import oauth.signpost.OAuth;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
import oauth.signpost.commonshttp.CommonsHttpOAuthProvider;
 import android.os.Bundle;
import android.os.Handler;
 import android.preference.PreferenceManager;
   import android.provider.SyncStateContract.Constants;
 import android.app.Activity;
 import android.content.Intent;
 import android.content.SharedPreferences;
 import android.content.SharedPreferences.Editor;
 import android.util.Log;
 import android.view.Menu;
 import android.view.View;
 import android.widget.Button;
 import android.widget.TextView;
  import android.widget.Toast;

  public class MainActivity extends Activity {


private SharedPreferences prefs;
  private final Handler mTwitterHandler = new Handler();
  private TextView loginStatus;

    final Runnable mUpdateTwitterNotification = new Runnable() {
     public void run() {
        Toast.makeText(getBaseContext(), "Tweet sent !", Toast.LENGTH_LONG).show();
      }
  };

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    this.prefs = PreferenceManager.getDefaultSharedPreferences(this);

    loginStatus = (TextView)findViewById(R.id.ls);
    Button tweet = (Button) findViewById(R.id.tweet);
    Button clearCredentials = (Button) findViewById(R.id.cc);

      tweet.setOnClickListener(new View.OnClickListener() {

     * to the twitter login page. Once the user authenticated, he'll authorize the     Android     application to send
    * tweets on the users behalf.
    */
        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);
         }
        }
    });

    clearCredentials.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
         clearCredentials();
         updateLoginStatus();
        }
    });
   }

  @Override
  protected void onResume() {
 super.onResume();
 updateLoginStatus();
  }

 public void updateLoginStatus() {
  loginStatus.setText("Logged into Twitter : " + TwitterUtils.isAuthenticated(prefs));
  }


 private String getTweetMsg() {
 return "Tweeting from Android App at " + new Date().toLocaleString();
  } 

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

 private void clearCredentials() {
 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
 final Editor edit = prefs.edit();
 edit.remove(OAuth.OAUTH_TOKEN);
 edit.remove(OAuth.OAUTH_TOKEN_SECRET);
 edit.commit();
 }
 }



import oauth.signpost.OAuthConsumer;
import oauth.signpost.OAuthProvider;
 import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
 import android.util.Log;


public class OAuthRequestTokenTask extends AsyncTask<Void, Void, Void> {

    final String TAG = getClass().getName();
    private Context context;
    private OAuthProvider provider;
    private OAuthConsumer consumer;

    /**
    *
    * We pass the OAuth consumer and provider.
    *
    * @param context
    * Required to be able to start the intent to launch the browser.
    * @param provider
    * The OAuthProvider object
    * @param consumer
    * The OAuthConsumer object
    */
    public OAuthRequestTokenTask(Context context,OAuthConsumer consumer,OAuthProvider provider) {
    this.context = context;
    this.consumer = consumer;
    this.provider = provider;
    }

    /**
    *
    * Retrieve the OAuth Request Token and present a browser to the user to authorize the token.
    *
    */
    @Override
    protected Void doInBackground(Void... params) {

    try {
    Log.i(TAG, "Retrieving request token from Google servers");
    final String url = provider.retrieveRequestToken(consumer, constants.OAUTH_CALLBACK_URL);
    Log.i(TAG, "Popping a browser with the authorize URL : " + url);
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)).setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_FROM_BACKGROUND);
    context.startActivity(intent);
    } catch (Exception e) {
    Log.e(TAG, "Error during OAUth retrieve request token", e);
    }

    return null;
    }

    }




import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.SyncStateContract.Constants;
import android.util.Log;
import oauth.signpost.OAuth;
import oauth.signpost.OAuthConsumer;
import oauth.signpost.OAuthProvider;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
import oauth.signpost.commonshttp.CommonsHttpOAuthProvider;

 public class PrepareRequestTokenActivity extends Activity {
final String TAG = getClass().getName();

private OAuthConsumer consumer;
private OAuthProvider provider;

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

  /**
  * Called when the OAuthRequestTokenTask finishes (user has authorized the request    token).
 * The callback URL will be intercepted here.
  */
 @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.
       */
      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,MainActivity.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);
     }
     }
    }

twitterUtils.java

import oauth.signpost.OAuth;
import twitter4j.Twitter;
 import twitter4j.TwitterException;
 import twitter4j.TwitterFactory;
 import twitter4j.http.AccessToken;
 import android.content.SharedPreferences;
 import android.provider.SyncStateContract.Constants;

  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(constants.CONSUMER_KEY, constants.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(constants.CONSUMER_KEY, constants.CONSUMER_SECRET);
  twitter.setOAuthAccessToken(a);
    twitter.updateStatus(msg);
 }  
 }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T10:00:50+00:00Added an answer on June 17, 2026 at 10:00 am

    Change Your Call Back URL and write below Call back URL instead of your URL.

    public static final String CALLBACK_URL = "x-oauthflow-twitter://callback";
    

    And see below link for more information.

    Twitter Integration in Android

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I am trying to render a haml file in a javascript response like so:
We're building an app, our first using Rails 3, and we're having to build
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am using Paperclip to handle profile photo uploads in my app. They upload
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am using jsonparser to parse data and images obtained from json response. When
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.