Android Twitter Login button not working

7.1k Views Asked by At

I'm writing an application that includes a sign in with twitter button. The button is okay. It shows on the screen when I click I give auth but I cant save the token and secret to a string variable. I mean it is empty always. The code at the below has been created by fabric.io automatically and I have added some stuff in it but it is not working. There is no error it just doesn't change the variable token and secret. I have tried this by an TextView object that changes in the success case and it never changes. What should I do? I just want to save token and secret to get user's 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
        }
    });

EDİT: I'm taking an error like the following, if I click the sign in with 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 simple to start a new activity when "success" happens automatically.

4

There are 4 best solutions below

0
On
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, the problem is not there. I write you example how i have done it.

0
On

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
On

Try to add the below code in the manifest file

<activity android:name="com.twitter.sdk.android.core.identity.OAuthActivity" />
0
On

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