如何使用Twitter API登录后获取用户数据

时间:2017-10-07 09:09:41

标签: java android rest twitter twitter-oauth

我使用Twitter API通过twitter进行简单登录,以获取一些用户的数据。在javaDocs中,有一个用户模型,其中包含用户的所有信息。我想要访问这些数据。我只是无法弄清楚如何真正得到它们。!?

我的代码:

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) + "\"");
    }

  }

logcat

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

1 个答案:

答案 0 :(得分:1)

经过大量研究后,我发现了它。 Twitter Kit提供了一种访问其api请求的简便方法。

当我成功登录时,我得到TwitterSession。然后使用此会话我可以设置TwitterApiClient并使用AccountService.verifyCredentials(),它为我提供了User对象及其所有数据。

这里是代码:

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