I am trying to integrate twitter login.
The login button of twitter is grayed out.
I have read somewhere that i have to add these line before setting content view to make it work:
TwitterAuthConfig authConfig = new TwitterAuthConfig(
getString(R.string.twitter_consumer_key),
getString(R.string.twitter_consumer_secret));
Fabric.with(this, new Twitter(authConfig));//here,i am having problem
setContentView(R.layout.activity_cover);
But android studio does not recognize 'Fabric' .It has red color.This is the code:
Fabric.with(this, new Twitter(authConfig));
Also, there is a red underline below 'Twitter',and i have the following error:
'Twitter(com.twitter.sdk.android.core.TwitterConfig)' has private
access in 'com.twitter.sdk.android.core.Twitter'
Also,i have tried fabric plugin of android studio,but there is no option for twitter login:
Instead of
Fabric.with(this, new Twitter(authConfig));
write this:
TwitterConfig.Builder builder=new TwitterConfig.Builder(this);
builder.twitterAuthConfig(authConfig);
Twitter.initialize(builder.build());
Also,you need to add this line in gradle(app level) :
compile 'com.twitter.sdk.android:twitter:3.0.0'
Add dependencies and repositories in build.gradle
dependencies {
compile 'com.twitter.sdk.android:twitter-core:3.1.0'
compile 'com.twitter.sdk.android:tweet-ui:3.1.0'
compile 'com.twitter.sdk.android:tweet-composer:3.1.0'
compile 'com.twitter.sdk.android:twitter-mopub:3.1.0'
}
repositories {
jcenter()
}
Initialize Twitter Kit in oncreate() of Activity
Twitter.initialize(this);
Add your API key and secret to your application resources.
<resources>
<string android:name="com.twitter.sdk.android.CONSUMER_KEY">XXXXXXXXXXX</string>
<string android:name="com.twitter.sdk.android.CONSUMER_SECRET">XXXXXXXXXXX</string>
</resources>
Add Twitter Button in xml
<com.twitter.sdk.android.core.identity.TwitterLoginButton
android:id="@+id/login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Initialize Twitter Button and get TwitterSession.. in success of that get User info
loginButton = (TwitterLoginButton) view.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
Log.e("result", "result " + result);
TwitterApiClient twitterApiClient = TwitterCore.getInstance().getApiClient();
AccountService accountService = twitterApiClient.getAccountService();
Call<User> call = accountService.verifyCredentials(true, true, true);
call.enqueue(new Callback<com.twitter.sdk.android.core.models.User>() {
@Override
public void success(Result<com.twitter.sdk.android.core.models.User> result) {
//here we go User details
Log.e("result", "result user " + result);
String imageUrl = result.data.profileImageUrl;
String email = result.data.email;
String userName = result.data.name;
System.out.println(imageUrl);
System.out.println(email);
System.out.println(userName);
}
@Override
public void failure(TwitterException exception) {
exception.printStackTrace();
}
});
}
@Override
public void failure(TwitterException exception) {
// Do something on failure
exception.printStackTrace();
}
});
If your button is in fragment, Add following code in activity
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//verificationFragment is my fragment
verificationFragment.onActivityResult(requestCode, resultCode, data);
}
Add following code in fragment
public 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);
}
Mike from Fabric here. Due to Google's acquisition of Fabric from Twitter, Twitter Kit is no longer available via Fabric. You should follow the instructions on their developer site. The changes you would need to change are as follows.
1) Remove Twitter from your Fabric init call.
2) Add the following to your build.gradle:
dependencies {
// Include all the Twitter APIs
compile 'com.twitter.sdk.android:twitter:3.0.0'
// (Optional) Monetize using mopub
compile 'com.twitter.sdk.android:twitter-mopub:3.0.0'
}
3) Make sure jcenter is in your repositories scope:
repositories {
jcenter()
}
4) Initialize Twitter in your Application's onCreate():
public class CustomApplication {
public void onCreate() {
Twitter.initialize(this);
}
}
5) Add your API key and secret to your resources:
<resources>
<string android:name="com.twitter.sdk.android.CONSUMER_KEY">XXXXXXXXXXX</string>
<string android:name="com.twitter.sdk.android.CONSUMER_SECRET">XXXXXXXXXXX</string>
</resources>