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 4614432
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:41:57+00:00 2026-05-22T01:41:57+00:00

I have an example where i am getting an 401 error because the consumer

  • 0

I have an example where i am getting an 401 error because the consumer is not correct or signature didn’t match .How to put signature key here because in my working java example there is a signature key and i want to implement it.

public class TwitterApp {
private Twitter mTwitter;
private TwitterSession mSession;
private AccessToken mAccessToken;
private OAuthConsumer mHttpOauthConsumer;
private OAuthProvider mHttpOauthprovider;
private String mConsumerKey;
private String mSecretKey;
private ProgressDialog mProgressDlg;
private TwDialogListener mListener;
private Context context;

public static final String CALLBACK_URL = "twitterapp://connect";
private static final String TAG = "TwitterApp";

public TwitterApp(Context context, String consumerKey, String secretKey) {
    this.context    = context;

    mTwitter        = new TwitterFactory().getInstance();
    mSession        = new TwitterSession(context);
    mProgressDlg    = new ProgressDialog(context);

    mProgressDlg.requestWindowFeature(Window.FEATURE_NO_TITLE);

    mConsumerKey    = consumerKey;
    mSecretKey      = secretKey;

    mHttpOauthConsumer = new CommonsHttpOAuthConsumer(mConsumerKey, mSecretKey);
    mHttpOauthprovider = new CommonsHttpOAuthProvider("http://twitter.com/oauth/request_token",
                                                 "http://twitter.com/oauth/access_token",
                                                 "http://twitter.com/oauth/authorize");

    mAccessToken    = mSession.getAccessToken();

    configureToken();
}

public void setListener(TwDialogListener listener) {
    mListener = listener;
}

@SuppressWarnings("deprecation")
private void configureToken() {
    if (mAccessToken != null) {
        mTwitter.setOAuthConsumer(mConsumerKey, mSecretKey);

        mTwitter.setOAuthAccessToken(mAccessToken);
    }
}

public boolean hasAccessToken() {
    return (mAccessToken == null) ? false : true;
}

public void resetAccessToken() {
    if (mAccessToken != null) {
        mSession.resetAccessToken();

        mAccessToken = null;
    }
}

public String getUsername() {
    return mSession.getUsername();
}

public void updateStatus(String status) throws Exception {
    try {
        mTwitter.updateStatus(status);
    } catch (TwitterException e) {
        throw e;
    }
}

public void authorize() {
    mProgressDlg.setMessage("Initializing ...");
    mProgressDlg.show();

    new Thread() {
        @Override
        public void run() {
            String authUrl = "";
            int what = 1;

            try {
                authUrl = mHttpOauthprovider.retrieveRequestToken(mHttpOauthConsumer, CALLBACK_URL);    

                what = 0;

                Log.d(TAG, "Request token url " + authUrl);
            } catch (Exception e) {
                Log.d(TAG, "Failed to get request token");

                e.printStackTrace();
            }

            mHandler.sendMessage(mHandler.obtainMessage(what, 1, 0, authUrl));
        }
    }.start();
}

public void processToken(String callbackUrl)  {
    mProgressDlg.setMessage("Finalizing ...");
    mProgressDlg.show();

    final String verifier = getVerifier(callbackUrl);

    new Thread() {
        @Override
        public void run() {
            int what = 1;

            try {
                mHttpOauthprovider.retrieveAccessToken(mHttpOauthConsumer, verifier);

                mAccessToken = new AccessToken(mHttpOauthConsumer.getToken(), mHttpOauthConsumer.getTokenSecret());

                configureToken();

                User user = mTwitter.verifyCredentials();

                mSession.storeAccessToken(mAccessToken, user.getName());

                what = 0;
            } catch (Exception e){
                Log.d(TAG, "Error getting access token");

                e.printStackTrace();
            }

            mHandler.sendMessage(mHandler.obtainMessage(what, 2, 0));
        }
    }.start();
}

private String getVerifier(String callbackUrl) {
    String verifier  = "";

    try {
        callbackUrl = callbackUrl.replace("twitterapp", "http");

        URL url         = new URL(callbackUrl);
        String query    = url.getQuery();

        String array[]  = query.split("&");

        for (String parameter : array) {
             String v[] = parameter.split("=");

             if (URLDecoder.decode(v[0]).equals(oauth.signpost.OAuth.OAUTH_VERIFIER)) {
                 verifier = URLDecoder.decode(v[1]);
                 break;
             }
        }
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }

    return verifier;
}

private void showLoginDialog(String url) {
    final TwDialogListener listener = new TwDialogListener() {
        @Override
        public void onComplete(String value) {
            processToken(value);
        }

        @Override
        public void onError(String value) {
            mListener.onError("Failed opening authorization page");
        }
    };

    new TwitterDialog(context, url, listener).show();
}

private Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        mProgressDlg.dismiss();

        if (msg.what == 1) {
            if (msg.arg1 == 1)
                mListener.onError("Error getting request token");
            else
                mListener.onError("Error getting access token");
        } else {
            if (msg.arg1 == 1)
                showLoginDialog((String) msg.obj);
            else
                mListener.onComplete("");
        }
    }
};

public interface TwDialogListener {
    public void onComplete(String value);       

    public void onError(String value);
}

}

i have an working example of java also where, i am getting the token easily but the thing is it uses DefaultOAuthConsumer and DefaultOAuthProvider but whenever i am trying to implement it in my example i am getting communication error, thats why i use CommonsHttpOAuthConsumer and CommonsHttpOAuthProvider.

here’s the java working code

public class Main {

public static void main(String[] args) throws Exception {

    OAuthConsumer consumer = new DefaultOAuthConsumer(
            "iIlNngv1KdV6XzNYkoLA",
            "exQ94pBpLXFcyttvLoxU2nrktThrlsj580zjYzmoM",
            SignatureMethod.HMAC_SHA1);

    OAuthProvider provider = new DefaultOAuthProvider(consumer,
            "http://twitter.com/oauth/request_token",
            "http://twitter.com/oauth/access_token",
            "http://twitter.com/oauth/authorize");

    System.out.println("Fetching request token from Twitter...");

    // we do not support callbacks, thus pass OOB
    String authUrl = provider.retrieveRequestToken(OAuth.OUT_OF_BAND);

    System.out.println("Request token: " + consumer.getToken());
    System.out.println("Token secret: " + consumer.getTokenSecret());

    System.out.println("Now visit:\n" + authUrl
            + "\n... and grant this app authorization");
    System.out.println("Enter the PIN code and hit ENTER when you're done:");

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String pin = br.readLine();

    System.out.println("Fetching access token from Twitter...");

    provider.retrieveAccessToken(pin);

    System.out.println("Access token: " + consumer.getToken());
    System.out.println("Token secret: " + consumer.getTokenSecret());

    URL url = new URL("http://twitter.com/statuses/mentions.xml");
    HttpURLConnection request = (HttpURLConnection) url.openConnection();

    consumer.sign(request);

    System.out.println("Sending request to Twitter...");
    request.connect();

    System.out.println("Response: " + request.getResponseCode() + " "
            + request.getResponseMessage());
}

}

and i am also not aware for call back url string i am using it here..

  • 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-05-22T01:41:58+00:00Added an answer on May 22, 2026 at 1:41 am

    The CALLBACK_URL should match the

    <intent-filter> 
    

    in your manifest.xml.

    In your code snippets, have you declared and initialized the mHttpOathCosumer and provider?

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

Sidebar

Related Questions

I have a try catch block to handle an error I am getting with
I have example of code below. <script type=text/javascript src=assets/scripts/somescript.php>. </script> So, will my browser
If I have example.com/dir and dir is basically a folder in the example.com server,
Anyone have an example snippet showing how to scale a Font using AffineTransform? Thanks.
I have an example in C# code, but it is using streamWriter . It
I have an example of some code that I see often in websites that
I have an example where a protocol would be ideal except for the fact
I have an example class containing two data points: public enum Sort { First,
I have an example database, it contains tables for Movies, People and Credits. The
I have an example function below that reads in a date as a string

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.