I am trying to implement facebook login written in kotlin. The following code works fine:
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import com.facebook.*
import com.facebook.login.LoginResult
import com.facebook.login.LoginManager
import com.facebook.login.widget.LoginButton
import org.json.JSONObject
import java.util.*
class MainActivity : AppCompatActivity() {
private var callbackManager: CallbackManager? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
callbackManager = CallbackManager.Factory.create()
var loginBtn: LoginButton = findViewById(R.id.login_button) as LoginButton
loginBtn.setReadPermissions(Arrays.asList("public_profile","email","user_photos"))
loginBtn.registerCallback(callbackManager, object : FacebookCallback<LoginResult> {
override fun onCancel() {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun onError(error: FacebookException) {
print("error" +error.message)
}
override fun onSuccess(result: LoginResult) {
GraphRequest.newMeRequest(result.accessToken, GraphRequest.GraphJSONObjectCallback {
obj: JSONObject, response: GraphResponse ->
println(obj.toString())
println(obj.getString("email"))})
println("successful" + result.accessToken)
}
})
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
callbackManager!!.onActivityResult(requestCode, resultCode, data)
}
}
I am trying to implement facebook login written in kotlin. The following code works fine:
package social.social
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import com.facebook.*
import com.facebook.login.LoginResult
import com.facebook.login.LoginManager
import com.facebook.login.widget.LoginButton
import org.json.JSONObject
import java.util.*
class MainActivity : AppCompatActivity() {
private var callbackManager: CallbackManager? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
callbackManager = CallbackManager.Factory.create()
var loginBtn: LoginButton = findViewById(R.id.login_button) as LoginButton
loginBtn.setReadPermissions(Arrays.asList("public_profile","email","user_photos"))
loginBtn.registerCallback(callbackManager, object : FacebookCallback<LoginResult> {
override fun onCancel() {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun onError(error: FacebookException) {
print("error" +error.message)
}
override fun onSuccess(result: LoginResult) {
GraphRequest.newMeRequest(result.accessToken, GraphRequest.GraphJSONObjectCallback {
obj: JSONObject, response: GraphResponse ->
println(obj.toString())
println(obj.getString("email"))})
println("successful" + result.accessToken)
}
})
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
callbackManager!!.onActivityResult(requestCode, resultCode, data)
}
}
I tried to add parameters to GraphRequest so I needed to store it into a variable like so:
val request: GraphRequest = GraphRequest.newMeRequest(result.accessToken, GraphRequest.GraphJSONObjectCallback {
obj: JSONObject, response: GraphResponse ->
println(obj.toString())
println(obj.getString("email"))})
request.executeAsync()
but now it fails build and I can’t figure out why
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
Unable to read class file: 'C:\projects\social\app\build\tmp\kotlin-classes\debug\social\social\MainActivity$onCreate$1$onSuccess$1.class'