Reputation: 813
I am creating an Android app. I am using Twitter Integration in Android.
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
My Work
I successfully implemented Login using Twitter.
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. I dont know how. Please tell how to call and get the Object.
Upvotes: 0
Views: 810
Reputation: 1416
An user object can be fetched with an API call via Retrofit. You do not have to include Retrofit explicitly in your project if you have already included the Twitter Kit.
Create Retrofit object with the new OAuth1aInterceptor
provided in the twitter-kit.
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();
Create an Interface
for the Retrofit Client
as usual.
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);
}
Call the ApiInterface
function 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-auth-config : This can be fetched from TwitterCore.getInstance().getAuthConfig()
screen-name : The screen-name
i.e the twitter-handle
of the logged-in user can be fetched from the current active session /*session*/.getUserName()
~ Twitter API Reference Index https://developer.twitter.com/en/docs/api-reference-index
Upvotes: 1