Android Fabric TwitterCore login without TwitterLo

2019-04-04 02:56发布

问题:

According to the TwitterCore documentation :

The simplest way to authenticate a user is using TwitterLoginButton.

How do you authenticate a user without the TwitterLoginButton?

回答1:

Take a look at TwitterAuthClient.

An example usage would be something like (where getCallingActivity() can be replaced with your calling Activity),

TwitterAuthClient twitterAuthClient = new TwitterAuthClient();
twitterAuthClient.authorize(getCallingActivity(), new Callback<TwitterSession>() {
        @Override
        public void success(final Result<TwitterSession> result) {
            final TwitterSession sessionData = result.data;
            // Do something with the returned TwitterSession (contains the user token and secret)

        }

        @Override
        public void failure(final TwitterException e) {
            // Do something on fail
        }
    });

Then delegate the onActivityResult to TwitterAuthClient,

twitterAuthClient.onActivityResult(requestCode, resultCode, data);


回答2:

You don't need to create a button for this. Basically, if you dig into some of the classes, you'll find that the code is relatively straightforward. So, here's how I did it. You just need to call TwitterAuthClient().authorize(Activity, Callback<TwitterSession)

The full code:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import com.twitter.sdk.android.core.Callback;
import com.twitter.sdk.android.core.Result;
import com.twitter.sdk.android.core.TwitterException;
import com.twitter.sdk.android.core.TwitterSession;
import com.twitter.sdk.android.core.identity.TwitterAuthClient;


/**
 * Created by Andrew on 6/23/15.
 */
public class ConnectTwitterActivity extends Activity {
    //The auth client itself
    /*you can abstract this and call TwitterCore.getInstance().login()
    but basically that call is doing this one.. 
    */
    TwitterAuthClient client;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //instanciate our client
        client = new TwitterAuthClient();
        //make the call to login 
        client.authorize(this, new Callback<TwitterSession>() {
            @Override
            public void success(Result<TwitterSession> result) {
                //feedback
                Toast.makeText(getApplicationContext(), "Login worked", Toast.LENGTH_LONG).show();
            }

            @Override
            public void failure(TwitterException e) {
                //feedback
                Toast.makeText(getApplicationContext(), "Login failed", Toast.LENGTH_LONG).show();
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //this method call is necessary to get our callback to get called. 
        client.onActivityResult(requestCode, resultCode, data);

    }
}


回答3:

You'd probably want to call the regular Twitter OAuth mechanism through something like Twitter4J. If you use the TwitterLoginButton this is all wrapped up for you via functions in Twitter Kit.



回答4:

If your goal is to not add a TwitterLoginButton in your layout(s), create a TwitterLoginButton programmatically and manually perform a click event:

TwitterLoginButton button = new TwitterLoginButton(this);

button.setCallback(new Callback<TwitterSession>() {
    @Override
    public void success(Result<TwitterSession> result) {
        // Do something with result, which provides a TwitterSession for making API calls
    }

    @Override
    public void failure(TwitterException exception) {
        // Do something on failure
    }
});

button.performClick();

The key point here being that once you create this button, you can call button.performClick() at your convenience.