简体   繁体   English

如何在Android SDK中使用Twitter Api获取用户对象?

[英]How to get User Object using Twitter Api in Android SDK?

I am creating an Android app. 我正在创建一个Android应用。 I am using Twitter Integration in Android. 我正在Android中使用Twitter集成。

My Need 我的需要
I want to access the User Object from Twitter stated here https://developer.twitter.com/en/docs/tweets/data-dictionary/overview/user-object 我想从Twitter访问访问此处的用户对象https://developer.twitter.com/en/docs/tweets/data-dictionary/overview/user-object

My Work 我的工作
I successfully implemented Login using Twitter. 我使用Twitter成功实现了Login。

This is my Main Activity Code :: 这是我的主要活动代码::

public class MainActivity extends AppCompatActivity {

    TwitterLoginButton loginButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        TwitterConfig config = new TwitterConfig.Builder(this)
                .logger(new DefaultLogger(Log.DEBUG))
                .twitterAuthConfig(new TwitterAuthConfig(getString(R.string.twitter_consumer_key), getString(R.string.twitter_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

                TwitterSession session = TwitterCore.getInstance().getSessionManager().getActiveSession();
                TwitterAuthToken authToken = session.getAuthToken();
                String token = authToken.token;
                String secret = authToken.secret;

                Intent intent =new Intent(getApplicationContext(), ProfilePage.class);
                intent.putExtra("token",token);
                startActivity(intent);






            }

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

My Issue: 我的问题:
Now I want to access the User Object of Twitter Api. 现在,我要访问Twitter Api的用户对象。 I dont know how. 我不知道 Please tell how to call and get the Object. 请说明如何调用和获取对象。

An user object can be fetched with an API call via Retrofit . 可以通过Retrofit使用API​​调用来获取用户对象。 You do not have to include Retrofit explicitly in your project if you have already included the Twitter Kit. 如果您已经包含Twitter Kit,则不必在项目中明确包含Retrofit。

  1. Create Retrofit object with the new OAuth1aInterceptor provided in the twitter-kit. 使用twitter-kit中提供的新OAuth1aInterceptor创建Retrofit对象。

      retrofit = new Retrofit.Builder() .baseUrl("https://api.twitter.com/1.1/") .addConverterFactory(GsonConverterFactory.create()) // Twitter interceptor .client(new OkHttpClient.Builder() .addInterceptor(new OAuth1aInterceptor(/*twitter-session*/, /*twitter-auth-config*/)) .build()) .build(); 
  2. Create an Interface for the Retrofit Client as usual. 照常为Retrofit Client创建一个Interface

      import com.twitter.sdk.android.core.models.User; import retrofit2.Call; import retrofit2.http.GET; import retrofit2.http.Query; public interface ApiInterface { @GET("users/show.json") Call<User> getUserDetails(@Query("screen_name") String screenName); } 
  3. Call the ApiInterface function getUserDetails() 调用ApiInterface函数getUserDetails()

      ApiInterface apiInterface = retrofit.create(ApiInterface.class); Call<User> call = apiInterface.getUserDetails(/*screen-name*/); call.enqueue(new Callback<User>() { @Override public void onResponse(Call<User> call, Response<User> response) { } @Override public void onFailure(Call<User> call, Throwable t) { } }); 

twitter-session : This can be fetched from TwitterCore.getInstance().getSessionManager().getActiveSession() . twitter-session:可以从TwitterCore.getInstance().getSessionManager().getActiveSession()

twitter-auth-config : This can be fetched from TwitterCore.getInstance().getAuthConfig() twitter-auth-config:可以从TwitterCore.getInstance().getAuthConfig()

screen-name : The screen-name ie the twitter-handle of the logged-in user can be fetched from the current active session /*session*/.getUserName() screen-name:可以从当前活动会话/*session*/.getUserName()获取screen-nametwitter-handle登录用户的twitter-handle /*session*/.getUserName()

~ Twitter API Reference Index https://developer.twitter.com/en/docs/api-reference-index 〜Twitter API参考索引https://developer.twitter.com/en/docs/api-reference-index

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM