Twitter Social Login using Kotlin

In fetchtwitteremail function, i get error toast “Request Failure”.
I’m not sure what is error?
Please help me.

binding.cvTwitter.setSafeOnClickListener { signUpwithTwitter() }

fun twitterInitialize() {
val config = TwitterConfig.Builder(this)
.twitterAuthConfig(TwitterAuthConfig(“", "”))
.debug(true)
.build()
Twitter.initialize(config)
twitterAuthClient = TwitterAuthClient()
}

fun signUpwithTwitter() {
if (getTwitterSession() == null) {
twitterAuthClient!!.authorize(this, object : Callback() {
override fun success(twitterSessionResult: Result) {
Toast.makeText(applicationContext, “Success”, Toast.LENGTH_SHORT).show()
val twitterSession = twitterSessionResult.data
fetchTwitterEmail(twitterSession)
}

override fun failure(e: TwitterException) {
Toast.makeText(applicationContext, “Failure”, Toast.LENGTH_SHORT).show()
}
})
} else {//if user is already authenticated direct call fetch twitter email api
fetchTwitterEmail(getTwitterSession())
}
}

private fun getTwitterSession(): TwitterSession? {
return TwitterCore.getInstance().sessionManager.activeSession
}

fun fetchTwitterEmail(twitterSession: Any?) {
if (twitterSession is TwitterSession) {
val session = twitterSession as? TwitterSession
twitterAuthClient?.requestEmail(session, object : Callback() {
override fun success(result: Result) {
sessionManager.twitterId = session!!.userId.toString()
sessionManager.userName = session!!.userName
sessionManager.userEmail = result.data
sessionManager.googleId = “”
sessionManager.appleId = “”
sessionManager.facebookId = “”
var token = session.userId.toString()
var str = “Now you are successfully login with twitter \n\n”
var tokenStr = “”
var usernameStr = “”
var emailStr = “”
if (token != null && token != “”) {
tokenStr = "User Id : " + token + “\n\n”
}

if (sessionManager.userName != null || sessionManager.userName != “”) {
usernameStr = "Username : " + sessionManager.userName + “\n\n”
}
Toast.makeText(applicationContext, “Succeed in authenticate.”, Toast.LENGTH_SHORT)
.show()
}
override fun failure(exception: TwitterException) {
val errorMessage = “Failed to authenticate. Error: ${exception.message}”
Toast.makeText(applicationContext, errorMessage, Toast.LENGTH_SHORT)
.show()
}
})
}
}

Can you please fix your formatting, and tell us what library (or libraries) you are using? This seems less like a general Kotlin problem, and more like a specific problem with the library you are using.

Thank you @Skater901. I think this is not related version of libraries.
How to set Redirect URI and Website URL.
I think this is the main cause.

I still can’t read your code. Can you format it, and maybe point out exactly which part is failing?

This is initialize part.

This is when button pressed.


image

this is fetchtwitteremail function for get twitteremail

My error is when twitterAuthClient.requestEmail() don’t go success, always go to failure part.

Ok, well I don’t know anything about the code that you’re using. My advice would be to put a break point in your failure function, and thoroughly inspect the TwitterException. What’s the stack trace, does it have a cause exception? If it has a cause exception, what’s the message of the cause exception? If the exception message is simply “Request Failure”, that gives you basically zero information about what the actual failure is.

I also don’t see anywhere in your code that you’re setting URIs or URLs, so what makes you say this?

Redirect URI and Website URLs are defined on my X developer account.
When I make breakpoint on “override fun failure” then It goes to Toast (errorMessage)
It says “Failed to authenticate. Error : Request Failure”