2017-10-21 99 views
0

我正在創建一個Android應用程序。我在Android中使用Twitter集成。如何使用Android SDK中的Twitter Api獲取用戶對象?

我的需要
我要訪問的用戶對象從Twitter這裏https://developer.twitter.com/en/docs/tweets/data-dictionary/overview/user-object

說我的工作
我成功地使用Twitter來實現登錄。

這是我的主要活動代碼::

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

我的問題:
現在我要訪問Twitter的API的用戶對象。我不知道如何。請告訴如何調用並獲取對象。

回答

1

用戶對象可以通過Retrofit的API調用獲取。 如果您已包含Twitter工具包,則不必在項目中明確包含Retrofit。

  1. 使用twitter-kit中提供的新OAuth1aInterceptor創建改造對象。

    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. 創建用於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. 調用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的會議:這可以從TwitterCore.getInstance().getSessionManager().getActiveSession()獲取。

嘰嘰喳喳-AUTH-配置:這可以從TwitterCore.getInstance().getAuthConfig()

屏幕名稱獲取:screen-name即的twitter-handle登錄的用戶可以從當前活動的會話中獲取/*session*/.getUserName()

〜Twitter API參考索引https://developer.twitter.com/en/docs/api-reference-index

相關問題