I run the login in the LoginActivity here (NB: that’s Kotlin!):
class LoginActivity: Activity() {
private val TAG = "LoginActivity"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val mOktaAuth = OktaAppAuth.getInstance(this)
if (mOktaAuth.isUserLoggedIn) {
Log.i(TAG, "User is already authenticated, proceeding to token activity")
startActivity(Intent(this, MainActivity::class.java))
finish()
return
}
mOktaAuth.init(this,
object : OktaAppAuth.OktaAuthListener {
override fun onSuccess() {
login(mOktaAuth)
}
override fun onTokenFailure(ex: AuthorizationException) {
Log.d(TAG, ex.error)
}
})
}
private fun login(mOktaAuth: OktaAppAuth) {
val login = mOktaAuth.isUserLoggedIn
if (!login) {
val completionIntent = Intent(this@LoginActivity, MainActivity::class.java)
val cancelIntent = Intent(this@LoginActivity, LoginActivity::class.java)
cancelIntent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
mOktaAuth.login(this@LoginActivity,
PendingIntent.getActivity(this@LoginActivity, 0, completionIntent, 0),
PendingIntent.getActivity(this@LoginActivity, 0, cancelIntent, 0)
)
}
}
}
I correctly get redirected to MainActivity, but whenever i call performAuthorizedRequest
NullPointerException
is thrown:
02-21 00:49:09.703 14496-14496/it.projector.lamba.projector E/AndroidRuntime:
FATAL EXCEPTION: main
Process: it.projector.lamba.projector, PID: 14496
java.lang.RuntimeException: Unable to start activity ComponentInfo{it.projector.lamba.projector/it.projector.lamba.projector.activities.MainActivity}: java.lang.NullPointerException: service cannot be null
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.NullPointerException: service cannot be null
at net.openid.appauth.Preconditions.checkNotNull(Preconditions.java:55)
at net.openid.appauth.AuthState.performActionWithFreshTokens(AuthState.java:523)
at net.openid.appauth.AuthState.performActionWithFreshTokens(AuthState.java:452)
at com.okta.appauth.android.OktaAppAuth.performAuthorizedRequest(OktaAppAuth.java:416)
at it.projector.lamba.projector.BackendService$Factory.getCurrentUser(BackendService.kt:37)
at it.projector.lamba.projector.activities.MainActivity.onCreate(MainActivity.kt:45)
at android.app.Activity.performCreate(Activity.java:7009)
at android.app.Activity.performCreate(Activity.java:7000)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
I Have no Idea why Furthermore, adding "offline_access"
to the scopes in the JSON file makes Okta refuse to start the login and the authentication process fails so i had to remove it.