Android Twitter login button not working

I am writing an application that includes a Twitter button sign. The button is ok. It is displayed on the screen when I click "I give auth", but I can not save the token and secret for the string variable. I mean, it's always empty. The code below was created by fabric.io automatically, and I added some things to it, but it does not work. There is no mistake, it just does not change the variable token and secret. I tried this with a TextView object that changes if successful, and it never changes. What should I do? I just want to keep the token and secret in order to get user information another time.

loginButton = (TwitterLoginButton) findViewById(R.id.twitter_login_button); loginButton.setCallback(new Callback<TwitterSession>() { @Override public void success(Result<TwitterSession> result) { System.out.println("Okay we are logged in now!"); TwitterSession session = Twitter.getSessionManager().getActiveSession(); TwitterAuthToken authToken = session.getAuthToken(); token = authToken.token; secret = authToken.secret; String answers = ""; User user = new User(userid,token,secret,answers); registerUser(user); twitterauth = true; } @Override public void failure(TwitterException exception) { // Do something on failure } }); 

EDIT: I am making a mistake, such as if I press the Twitter button again:

 2660-2660/com.website.nameofmyapp E/Twitter﹕ Authorization completed with an error com.twitter.sdk.android.core.TwitterAuthException: Authorize failed. at com.twitter.sdk.android.core.identity.TwitterAuthClient.handleAuthorize(TwitterAuthClient.java:110) at com.twitter.sdk.android.core.identity.TwitterAuthClient.authorize(TwitterAuthClient.java:101) at com.twitter.sdk.android.core.identity.TwitterLoginButton$LoginClickListener.onClick(TwitterLoginButton.java:161) at android.view.View.performClick(View.java:5184) at android.view.View$PerformClick.run(View.java:20910) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:145) at android.app.ActivityThread.main(ActivityThread.java:5942) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195) 

All I want to do is just start a new activity when “success” happens automatically.

+4
source share
4 answers

Two points helped me in this -

1- In MainActivity.java add this

  TwitterAuthConfig authConfig = new TwitterAuthConfig(TWITTER_KEY, TWITTER_SECRET); Fabric.with(this, new Twitter(authConfig)); @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); FragmentManager fragment = getSupportFragmentManager(); if (fragment != null) { fragment.findFragmentByTag("TwitterLogin").onActivityResult(requestCode, resultCode, data); } else Log.d("Twitter", "fragment is null"); } 

2- In your fragment class add this -

 @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); loginButton.onActivityResult(requestCode, resultCode, data); } 
+6
source

Try adding the code below to the manifest file

 <activity android:name="com.twitter.sdk.android.core.identity.OAuthActivity" /> 
+1
source
 loginButton = (TwitterLoginButton) findViewById(R.id.twitter_login_button); loginButton.setCallback(new Callback<TwitterSession>() { @Override public void success(Result<TwitterSession> result) { TwitterSession session = Twitter.getSessionManager().getActiveSession(); TwitterAuthToken authToken = session.getAuthToken(); token = authToken.token; secret = authToken.secret; } @Override public void failure(TwitterException exception) { } }); 

it looks like your code is good, no problem. I am writing you an example of how I did this.

0
source

Add before setContentView (R.layout.activity) in onCreate

 TwitterAuthConfig authConfig = new TwitterAuthConfig( getString(R.string.twitter_consumer_key), getString(R.string.twitter_consumer_secret)); TwitterConfig twitterConfig = new TwitterConfig.Builder(this) .twitterAuthConfig(authConfig) .build(); Twitter.initialize(twitterConfig); 
0
source

All Articles