How to get user data after login with Twitter API

Viewed 546

I used Twitter API to do a simple login with twitter to get a few of the user's data. In the javaDocs, there's a User model which contains all the info for a user. I want access to these data. I just can't figure out how to actually get them.!?

My code:

public class MainActivity extends AppCompatActivity {

  public TwitterLoginButton loginButton;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Twitter.initialize(this);

    TwitterConfig config = new TwitterConfig.Builder(this)
        .logger(new DefaultLogger(Log.DEBUG))
        .twitterAuthConfig(
            new TwitterAuthConfig(getString(R.string.com_twitter_sdk_android_CONSUMER_KEY),
                getString(R.string.com_twitter_sdk_android_CONSUMER_SECRET)))
        .debug(true)
        .build();
    Twitter.initialize(config);
    setContentView(R.layout.activity_main);

    loginButton = (TwitterLoginButton) findViewById(R.id.login_button);
    loginButton.setCallback(new Callback<TwitterSession>() {
      @Override
      public void success(Result<TwitterSession> result) {
        // Do something with result, which provides a TwitterSession for making API calls
        Log.i("Twitter response  ", result.data.getUserName());
       }

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


  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    // Pass the activity result to the login button.
    loginButton.onActivityResult(requestCode, resultCode, data);

    for (String key : data.getExtras().keySet()) {
      Log.d("Bundle Debug", key + " = \"" + data.getExtras().get(key) + "\"");
    }

  }

The logcat

// logcat: Bundle Debug log 
 user_id = "519****"
 screen_name = "Alaa****"
 tk = "519644856************************"
 ts = "s0RkdolPk************************"
1 Answers

After a lot of research, I figured it out. Twitter Kit provides an easy way to access its api requests.

When I login successfully I get a TwitterSession. then using this session I can set TwitterApiClient and use the AccountService.verifyCredentials() which gives me the User object with all its data.

here's the code:

loginButton.setCallback(new Callback<TwitterSession>() {
  @Override
  public void success(Result<TwitterSession> result) {

    TwitterApiClient twitterApiClient = TwitterCore.getInstance().getApiClient();
    AccountService service = twitterApiClient.getAccountService();

    service.verifyCredentials(false, false, false).enqueue(new Callback<User>() {
      @Override
      public void success(Result<User> result) {

          // I converted the user object into json for readability purposes
          User user = result.data;
          Gson gson = new Gson();
          String jsonData = gson.toJson(user);
          Log.i("Json> User  ", jsonData + "");
      }

      @Override
      public void failure(TwitterException exception) {
      }
    });
  }

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