Twitter Login SDK is android library which will help you to integrate social login through twitter with default button and custom button. It will help you to complete your integration in small step. Just create library class instance and use it or also modify ui as your requirement.
Step 1. Add the JitPack repository to your build file
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
Step 2. Add the dependency
dependencies {
implementation 'com.github.ankurbhut:TwiiterSdk:release_version'
}
Step 3. Add This initialization in Application Class.
val config = TwitterConfig.Builder(this)
.logger(DefaultLogger(Log.DEBUG)) //enable logging when app is in debug mode
.twitterAuthConfig(
TwitterAuthConfig(
resources.getString(R.string.com_twitter_sdk_android_CONSUMER_KEY),
resources.getString(R.string.com_twitter_sdk_android_CONSUMER_SECRET)
)
) //pass the created app Consumer KEY and Secret also called API Key and Secret
.debug(true) //enable debug mode
.build()
To initialize the sdk class, Use below code and setListeners to receive the callback.
private val twitterLogin = TwitterLogin()
twitterLogin.setTwitterLoginListenerCallback(this) // set callback listener
twitterLoginButton = findViewById(R.id.default_twitter_login_button)
twitterCustomLoginButton = findViewById(R.id.custom_twitter_login_button)
userDetailsLabel = findViewById(R.id.txtData)
twitterLogin.loginWithDefaultButton(twitterLoginButton) // login with default twitter button
// login with custom button
twitterCustomLoginButton.setOnClickListener {
twitterLogin.login(this)
}
Add Below Method to get success login data and implement CallbackListener.
override fun onTwitterSuccess(response: TwitterLoginResponse) {
// Handle login response here...
userDetailsLabel.text =
String.format("User Id : ${response.id} \nScreen Name : ${response.name} \nEmail Id : ${response.email} ".trimIndent())
}
override fun onTwitterFailure(error: Exception) {
// Handle exception here...
Toast.makeText(
this@MainActivity,
"Failed to authenticate. Please try again.",
Toast.LENGTH_SHORT
).show()
}
Pass ActivityResult to Library for handle response
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
twitterLogin.onActivityResult(requestCode, resultCode, data)
}
Maintained by Ankur Bhut
- Bug reports and pull requests are welcome.
- Make sure you use square/java-code-styles to format your code.