Create
Fabric account and Twitter App
Step – 1 follow this link to create fabric account.
Step – 2 follow this link to login after complete sign up process.
Step – 3 Create New app in Twitter apps.
Step – 4 Go to application management and navigate Keys and Access Tokens.
Step – 5 Copy and Paste your consumer key and
secret key.
Step – 6 Now create new app in fabric studio.
Step – 7 Select Twitter from All Kits and install it
Step – 8 Paste here your consumer key and secret key from your app
which you copied and now you got code which set in your main activity.
Step – 9 Now put this Studio CODE if you don't directly get.
package com..testtwitterlogin;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import com.twitter.sdk.android.Twitter;
import com.twitter.sdk.android.core.Callback;
import com.twitter.sdk.android.core.Result;
import com.twitter.sdk.android.core.TwitterAuthConfig;
import com.twitter.sdk.android.core.TwitterException;
import com.twitter.sdk.android.core.TwitterSession;
import com.twitter.sdk.android.core.identity.TwitterLoginButton;
import com.twitter.sdk.android.core.models.User;
import io.fabric.sdk.android.Fabric;
public class MainActivity extends Activity {
private static final String TWITTER_KEY = "your twitter consumer key";
private static final String TWITTER_SECRET = "your twitter secret key";
private TwitterLoginButton loginButton;
TextView textView;
TwitterSession session;
@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);
textView =
(TextView) findViewById(R.id.tv_username);
loginButton =
(TwitterLoginButton) findViewById(R.id.twitter_login_button);
loginButton.setCallback(new Callback<TwitterSession>() {
@Override
public void success(Result<TwitterSession>
result) {
session = result.data;
String username = session.getUserName();
Long
userid = session.getUserId();
textView.setText("Hi " +
username + "\n id:" + userid);
}
@Override
public void failure(TwitterException
exception) {
Log.d("TwitterKit",
"Login with Twitter failure", exception);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode,
resultCode, data);
loginButton.onActivityResult(requestCode,
resultCode, data);
}
void getUserData() {
Twitter.getApiClient(session).getAccountService()
.verifyCredentials(true, false, new Callback<User>() {
@Override
public void failure(TwitterException
e) {
}
@Override
public void success(Result<User>
userResult) {
User user =
userResult.data;
String twitterImage
= user.profileImageUrl;
try {
Log.e("imageurl",
user.profileImageUrl);
Log.e("name",
user.name);
// Log.e("email",user.email);
Log.e("des", user.description);
Log.e("followers ",
String.valueOf(user.followersCount));
Log.e("createdAt",
user.createdAt);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
Final Output
Feel free to post your comments, queries and suggestions
Cheers...:-)