I'm trying out to get profile image from Twitter, I don't know what method I need to call to get url of profile pic of the person who authorized the app, Here is My code:
MainActivity.class
public class MainActivity extends AppCompatActivity {
private static final String TWITTER_KEY = "TWITTER_KEY";
private static final String TWITTER_SECRET = "TWITTER_SECRET";
private TwitterLoginButton loginButton;
private long TwitterID;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TwitterAuthConfig authConfig = new TwitterAuthConfig(TWITTER_KEY, TWITTER_SECRET);
Fabric.with(this, new Twitter(authConfig));
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
loginButton = (TwitterLoginButton) findViewById(R.id.twitter_login_button);
loginButton.setCallback(new Callback<TwitterSession>() {
@Override
public void success(Result<TwitterSession> result) {Twitter.getInstance().core.getSessionManager().getActiveSession()
TwitterSession session = result.data;
TwitterID = session.getId();
String msg = "@" + session.getUserName() + " logged in! (#" + session.getUserId() + ")" + "\nid = " + TwitterID;
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
}
@Override
public void failure(TwitterException exception) {
Toast.makeText(getApplicationContext(), "Keep it up rest is piece of Cake", Toast.LENGTH_LONG).show();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
loginButton.onActivityResult(requestCode, resultCode, data);
}
}
What should be the code added to get the Profile Image Url? And also please do tell that if this the right way to do things for adding twitter login.
I cannot say any thing final regarding the implementation, apparently it is fine, but that actually depends on your requirements and application running environment. If you have followed the documentation properly and it's working fine on few devices with different accounts, then it is definitely fine.
For profile picture, as the documentation says:
You can obtain a user’s most recent profile image, along with the other components comprising their identity on Twitter, from GET users/show. Within the user object, you’ll find the profile_image_url and profile_image_url_https fields. These fields will contain the resized “normal” variant of the user’s uploaded image. This “normal” variant is typically 48px by 48px.
I believe this is the right way to get user details. But alternatively, if you don't want to get user details and keep the things simple, you can use a lot easier way to get the profile picture. (I cannot guarantee how long Twitter will be supporting this but yes it works perfect yet :) )
where username
is the Twitter Username of user you can get by calling session.getUserName()
in success
method com.twitter.sdk.android.core.Callback<TwitterSession>