Đây là cách tôi thực hiện Twitter đăng nhập với vải:
chính Declare twitter và bí mật:
private static final String TWITTER_KEY = "r5nPFPbcDrzoJM9bIBCqyfHPK";
private static final String TWITTER_SECRET = "oJ8y2KPIySPpoBX3eCcqgcnmPGXLI94BR4g9ZztnApSmXQG9Ij ";
//Twitter Login Button
TwitterLoginButton twitterLoginButton;
onCreate() phương pháp:
//Initializing TwitterAuthConfig, these two line will also added automatically while configuration we did
TwitterAuthConfig authConfig = new TwitterAuthConfig(TWITTER_KEY, TWITTER_SECRET);
Fabric.with(this, new Twitter(authConfig));
setContentView(R.layout.activity_main);
//Initializing twitter login button
twitterLoginButton = (TwitterLoginButton) findViewById(R.id.twitterLogin);
//Adding callback to the button
twitterLoginButton.setCallback(new Callback<TwitterSession>() {
@Override
public void success(Result<TwitterSession> result) {
//If login succeeds passing the Calling the login method and passing Result object
login(result);
}
@Override
public void failure(TwitterException exception) {
//If failure occurs while login handle it here
Log.d("TwitterKit", "Login with Twitter failure", exception);
}
});
3.override onActivityResult():
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//Adding the login result back to the button
twitterLoginButton.onActivityResult(requestCode, resultCode, data);
}
4. Cuối cùng, đăng nhập():
public void login(Result<TwitterSession> result) {
//Creating a twitter session with result's data
TwitterSession session = result.data;
//Getting the username from session
final String username = session.getUserName();
//This code will fetch the profile image URL
//Getting the account service of the user logged in
Twitter.getApiClient(session).getAccountService()
.verifyCredentials(true, false, new Callback<User>() {
@Override
public void failure(TwitterException e) {
//If any error occurs handle it here
}
@Override
public void success(Result<User> userResult) {
//If it succeeds creating a User object from userResult.data
User user = userResult.data;
//Getting the profile image url
String profileImage = user.profileImageUrl.replace("_normal", "");
Log.d("done","name-->"+username + "url-->"+profileImage);
// Toast.makeText(this,"name-->"+username + "url-->"+profileImage,Toast.LENGTH_LONG).show();
}
});
}
Bạn có username và url profilepicture trong login()
để sử dụng bất cứ nơi nào bạn muốn.
Nguồn
2016-06-07 11:28:23
kiểm tra liên kết sau: https://androidbeasts.wordpress.com/2015/07/22/twitter-fabric-integration/ – Aakash