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()
}
})
}
}