in Education by
I'm experiencing some problems regarding Twitter OAuth within an android activity. I read a lot of tutorials and example code, but I'm still not able to receive the access token. Everytime I try to authorize I'm getting this OAuthNotAuthorizedException: oauth.signpost.exception.OAuthNotAuthorizedException: Authorization failed (server replied with a 401). This can happen if the consumer key was not correct or the signatures did not match. I googled a lot and it felt like I read thousands of solutions, but none of them worked out for me. Hope you can help me with this one (and I'm not bothering your with the same old newbie question! ;) ) Here is my complete activity code: package de.ownor.moremote; import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer; import oauth.signpost.commonshttp.CommonsHttpOAuthProvider; import twitter4j.Twitter; import twitter4j.TwitterFactory; import twitter4j.auth.AccessToken; import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.net.Uri; import android.os.Bundle; import android.util.Log; import android.widget.Toast; public class MakeTweet extends Activity { public static final String CONSUMER_KEY = "Y0iYMkUgNX8kKgvDjzFgg"; public static final String CONSUMER_SECRET = "nuBZd8UGml5cjbm94Hf6dWOwIByrisZWpSnqODaUB5Q"; public static final String CALLBACK_URL = "de.ownor.moremote://twitter"; private static final String REQUEST_URL = "https://api.twitter.com/oauth/request_token"; private static final String ACCESS_TOKEN_URL = "https://api.twitter.com/oauth/access_token"; private static final String AUTH_URL = "https://api.twitter.com/oauth/authorize"; public static final String PREFS_NAME = "TwitterLogin"; public static final String TAG = "moremote - MakeTweet"; private Twitter twitter; private CommonsHttpOAuthProvider provider; private CommonsHttpOAuthConsumer consumer; @Override protected void onCreate(Bundle savedInstanceState) { System.setProperty("http.keepAlive", "false"); super.onCreate(savedInstanceState); setContentView(R.layout.make_tweet); twitter = null; if (!checkForSavedLogin()) { askOAuth(); } getConsumerProvider(); } @Override protected void onResume() { super.onResume(); System.out.println("RESUMING!!"); if (this.getIntent() != null && this.getIntent().getData() != null) { Uri uri = this.getIntent().getData(); if (uri != null && uri.toString().startsWith(CALLBACK_URL)) { String verifier = uri .getQueryParameter(oauth.signpost.OAuth.OAUTH_VERIFIER); try { // this will populate token and token_secret in consumer // ///////////////////////////// // exception is thrown here! // // ///////////////////////////// provider.retrieveAccessToken(consumer, verifier); // Get Access Token and persist it AccessToken a = new AccessToken(consumer.getToken(), consumer.getTokenSecret()); storeAccessToken(a); // initialize Twitter4J twitter = new TwitterFactory().getInstance(); twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET); twitter.setOAuthAccessToken(a); ((ApplicationEx) getApplication()).twitter = twitter; } catch (Exception e) { // Log.e(APP, e.getMessage()); e.printStackTrace(); Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG) .show(); } } } } private void askOAuth() { try { consumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET); provider = new CommonsHttpOAuthProvider(REQUEST_URL, ACCESS_TOKEN_URL, AUTH_URL); provider.setOAuth10a(true); String authUrl = provider.retrieveRequestToken(consumer, CALLBACK_URL); Toast.makeText(this, "Please authorize this app!", Toast.LENGTH_LONG).show(); setConsumerProvider(); startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl))); } catch (Exception e) { Log.e(TAG, "Error in askOAuth"); e.printStackTrace(); Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show(); } } private Boolean checkForSavedLogin() { // Get Access Token and persist it AccessToken a = getAccessToken(); if (a == null) { Log.d(TAG, "No saved Login found"); return false; } // initialize Twitter4J twitter = new TwitterFactory().getInstance(); twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET); twitter.setOAuthAccessToken(a); ((ApplicationEx) getApplication()).twitter = twitter; return true; } private AccessToken getAccessToken() { SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE); String token = settings.getString("accessTokenToken", ""); String tokenSecret = settings.getString("accessTokenSecret", ""); if (token != null && tokenSecret != null && !"".equals(tokenSecret) && !"".equals(token)) { return new AccessToken(token, tokenSecret); } return null; } private void storeAccessToken(AccessToken a) { SharedPreferences settings = Preferences.getPreferences(this); SharedPreferences.Editor editor = settings.edit(); editor.putString("accessTokenToken", a.getToken()); editor.putString("accessTokenSecret", a.getTokenSecret()); editor.commit(); } private void getConsumerProvider() { CommonsHttpOAuthProvider p = ((ApplicationEx) getApplication()).provider; if (p != null) { provider = p; } CommonsHttpOAuthConsumer c = ((ApplicationEx) getApplication()).consumer; if (c != null) { consumer = c; } } private void setConsumerProvider() { if (provider != null) { ((ApplicationEx) getApplication()).provider = provider; } if (consumer != null) { ((ApplicationEx) getApplication()).consumer = consumer; } } } The exception is thrown in the onResume()-Method. I marked the exact line. I really hope anyone can help me here. If you need further information just scream! Thanks! Simon. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
I finally managed to get the OAuth running. Unless I don't know the exact error. I guess it was some issue with the consumer. Here is my running code: onCreate: protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.twitter); pref = Preferences.getPreferences(this); consumer = new CommonsHttpOAuthConsumer(Key.TW_CONSUMER_KEY, Key.TW_CONSUMER_SECRET); provider = new CommonsHttpOAuthProvider(Key.TW_REQUEST_TOKEN_URL, Key.TW_ACCESS_TOKEN_URL, Key.TW_AUTHORIZE_URL); provider.setOAuth10a(true); [...] if (getIntent().getData() == null) { checkForSavedLogin(); } } onResume: protected void onResume() { super.onResume(); Uri uri = getIntent().getData(); if (uri != null && Key.TW_CALLBACK_URI.getScheme().equals(uri.getScheme())) { String token = pref.getString(Key.TW_REQUEST_TOKEN, null); String secret = pref.getString(Key.TW_REQUEST_SECRET, null); try { if (!(token == null || secret == null)) { consumer.setTokenWithSecret(token, secret); } String otoken = uri.getQueryParameter(OAuth.OAUTH_TOKEN); String verifier = uri.getQueryParameter(OAuth.OAUTH_VERIFIER); Assert.assertEquals(otoken, consumer.getToken()); provider.retrieveAccessToken(consumer, verifier); token = consumer.getToken(); secret = consumer.getTokenSecret(); saveAuthInformation(token, secret); // Delete request information saveRequestInformation(null, null); if (!(token == null || secret == null)) { Log.d(TAG, "Twitter login found!"); consumer.setTokenWithSecret(token, secret); twitter = new TwitterFactory().getInstance(); twitter.setOAuthConsumer(Key.TW_CONSUMER_KEY, Key.TW_CONSUMER_SECRET); twitter.setOAuthAccessToken(new AccessToken(token, secret)); } } catch (Exception e) { Log.d(TAG, "Couldn't receive token" + e.getMessage()); e.printStackTrace(); } } [...] } other methods: private void checkForSavedLogin() { if (pref.contains(Key.TW_USER_TOKEN) && pref.contains(Key.TW_USER_SECRET)) { token = pref.getString(Key.TW_USER_TOKEN, null); secret = pref.getString(Key.TW_USER_SECRET, null); if (!(token == null || secret == null)) { Log.d(TAG, "Twitter login found!"); consumer.setTokenWithSecret(token, secret); twitter = new TwitterFactory().getInstance(); twitter.setOAuthConsumer(Key.TW_CONSUMER_KEY, Key.TW_CONSUMER_SECRET); twitter.setOAuthAccessToken(new AccessToken(token, secret)); } } else { Log.d(TAG, "No Twitter login saved - asking for OAuth"); askOAuth(); } } private void askOAuth() { try { String authUrl = provider.retrieveRequestToken(consumer, Key.TW_CALLBACK_URI.toString()); saveRequestInformation(consumer.getToken(), consumer.getTokenSecret()); Toast.makeText(this, "Please authorize this app!", Toast.LENGTH_LONG).show(); this.startActivity(new Intent(Intent.ACTION_VIEW, Uri .parse(authUrl))); } catch (OAuthException e) { e.printStackTrace(); } } private void saveRequestInformation(String token, String secret) { // null means to clear the old values SharedPreferences.Editor editor = pref.edit(); if (token == null) { editor.remove(Key.TW_REQUEST_TOKEN); } else { editor.putString(Key.TW_REQUEST_TOKEN, token); } if (secret == null) { editor.remove(Key.TW_REQUEST_SECRET); } else { editor.putString(Key.TW_REQUEST_SECRET, secret); } editor.commit(); } private void saveAuthInformation(String token, String secret) { pref = Preferences.getPreferences(this); Editor editor = pref.edit(); editor.putString(Key.TW_USER_TOKEN, token); editor.putString(Key.TW_USER_SECRET, secret); editor.commit(); } Feel free to use this code... :)

Related questions

0 votes
    Animation example link: https://drive.google.com/open?id=1M5UBylFj0_8mtOEQT7jjsPN9DcCjyfPI I want to show my app ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    I have an PHP SDK for Twitter OAuth, but in it's constructor, I have to pass the OAuth secret ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 10, 2022 in Education by JackTerrance
0 votes
    I'm looking at making an custom Android object Serializable. Should be simple I just cannot find a easy ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Aug 1, 2022 in Education by JackTerrance
0 votes
    I've made a fresh eclipse reinstall + Android SDK v4.0. I am trying to create a new 4.0 AVD ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 30, 2022 in Education by JackTerrance
0 votes
    I'm trying to execute the following command: arp -a and I would like to redirect the output to a ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 26, 2022 in Education by JackTerrance
0 votes
    I've noticed on Marshmallow (e.g. Nexus 6P) and also on some more recently updated Lollipop phones ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    I'm trying to execute the following command: arp -a and I would like to redirect the output to a ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    i have this problem that i want to upload image on a server through multi-part form but its keep ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 8, 2022 in Education by JackTerrance
0 votes
    I want making multi-bluetooth from Android studio. I found multi-bluetooth java code libraries. but I am ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 3, 2022 in Education by JackTerrance
0 votes
    i want to run a task every 5 minutes. i've tried to solve it with an IntentService and ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 26, 2022 in Education by JackTerrance
0 votes
    i have this problem that i want to upload image on a server through multi-part form but its keep ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 19, 2022 in Education by JackTerrance
0 votes
    I am creating an application for Android. I am using Realm as a Database. Realm working fine in the ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 14, 2022 in Education by JackTerrance
0 votes
    I want to put a settings icon to the right of a full-width button which when clicked opens an ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
0 votes
    I just updated to Android Studio 3.4 (Canary 5). Then I opened my existing project (which worked ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 19, 2022 in Education by JackTerrance
0 votes
    How To Escape String Like Below. I Use Below Code But Say "The method unescape(String) is undefined ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 4, 2022 in Education by JackTerrance
...