How do you fix a Firebase Google Signin getTokenid return null?

3,902 views
Skip to first unread message

Nana Zimmer

unread,
Oct 11, 2016, 2:40:20 PM10/11/16
to Firebase Google Group
So for some reason, my app works with google sign in on some days and it doesn't on other days. Lately, I've been battling with 
java.lang.String com.google.android.gms.auth.api.signin.GoogleSignInAccount.getIdToken(). Now the last time it happened I had to delete my project and google cloud API's to then be able to connect without a token error. I'm at my wit's end would be nice to know a definite fix since I don't want to launch a prototype with this problem still persisting. Attached is my login code and my error log. 

LoginActivity:
/**
 * This method will return the results after the intent has started
 */

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
 
Log.d(TAG, "onActivityResult: "+ Auth.GoogleSignInApi.getSignInResultFromIntent(data).getSignInAccount());
 
switch (requestCode) {
 
case REQUEST_CODE_SIGN_IN:
 
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
 
GoogleSignInAccount account = result.getSignInAccount();
 firebaseAuthWithGoogle
(account);
 
break;
 
default:
 
super.onActivityResult(requestCode, resultCode, data);
 
break;
 
}

}


/**
 * Kick of google authentication through firebase
 * */

private void firebaseAuthWithGoogle(GoogleSignInAccount acct){
 
//show progressbar
 showProgressDialog
();

 
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
 mAuth
.signInWithCredential(credential)
 
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
 
@Override
 
public void onComplete(@NonNull Task<AuthResult> task) {
 
Log.d(TAG, "signWithCredential:onComplete:" + task.isSuccessful() + FirebaseAuth.getInstance().getCurrentUser());
 handleSignInSuccess
();
 hideProgressDialog
();
 
}
 
});

}


Error log:
D/FA: Logging event (FE): _e, Bundle[{_o=auto, _et=13259}]
V
/FA: Inactivity, disconnecting from the service
D
/LoginActivity: onActivityResult: null
D
/AndroidRuntime: Shutting down VM
E
/UncaughtException: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1006, result=0, data=Intent { (has extras) }} to activity {com.example.oseix.umc/com.example.oseix.umc.LoginActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.android.gms.auth.api.signin.GoogleSignInAccount.getIdToken()' on a null object reference
                         at android
.app.ActivityThread.deliverResults(ActivityThread.java:4053)
                         at android
.app.ActivityThread.handleSendResult(ActivityThread.java:4096)
                         at android
.app.ActivityThread.-wrap20(ActivityThread.java)
                         at android
.app.ActivityThread$H.handleMessage(ActivityThread.java:1516)
                         at android
.os.Handler.dispatchMessage(Handler.java:102)
                         at android
.os.Looper.loop(Looper.java:154)
                         at android
.app.ActivityThread.main(ActivityThread.java:6077)
                         at java
.lang.reflect.Method.invoke(Native Method)
                         at com
.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                         at com
.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
                     
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.android.gms.auth.api.signin.GoogleSignInAccount.getIdToken()' on a null object reference
                         at com
.example.oseix.umc.LoginActivity.firebaseAuthWithGoogle(LoginActivity.java:286)
                         at com
.example.oseix.umc.LoginActivity.onActivityResult(LoginActivity.java:269)
                         at android
.app.Activity.dispatchActivityResult(Activity.java:6917)
                         at android
.app.ActivityThread.deliverResults(ActivityThread.java:4049)
                         at android
.app.ActivityThread.handleSendResult(ActivityThread.java:4096)
                         at android
.app.ActivityThread.-wrap20(ActivityThread.java)
                         at android
.app.ActivityThread$H.handleMessage(ActivityThread.java:1516)
                         at android
.os.Handler.dispatchMessage(Handler.java:102)
                         at android
.os.Looper.loop(Looper.java:154)
                         at android
.app.ActivityThread.main(ActivityThread.java:6077)
                         at java
.lang.reflect.Method.invoke(Native Method)
                         at com
.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                         at com
.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
D
/FA: Logging event (FE): _ae, Bundle[{_o=crash, timestamp=1476203958303, fatal=1}]
V
/FA: Using measurement service
V
/FA: Connecting to remote service
D
/FirebaseCrashApiImpl: throwable java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1006, result=0, data=Intent { (has extras) }} to activity {com.example.oseix.umc/com.example.oseix.umc.LoginActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.android.gms.auth.api.signin.GoogleSignInAccount.getIdToken()' on a null object reference
E
/AndroidRuntime: FATAL EXCEPTION: main
                 
Process: com.example.oseix.umc, PID: 5920
                  java
.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1006, result=0, data=Intent { (has extras) }} to activity {com.example.oseix.umc/com.example.oseix.umc.LoginActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.android.gms.auth.api.signin.GoogleSignInAccount.getIdToken()' on a null object reference
                      at android
.app.ActivityThread.deliverResults(ActivityThread.java:4053)
                      at android
.app.ActivityThread.handleSendResult(ActivityThread.java:4096)
                      at android
.app.ActivityThread.-wrap20(ActivityThread.java)
                      at android
.app.ActivityThread$H.handleMessage(ActivityThread.java:1516)
                      at android
.os.Handler.dispatchMessage(Handler.java:102)
                      at android
.os.Looper.loop(Looper.java:154)
                      at android
.app.ActivityThread.main(ActivityThread.java:6077)
                      at java
.lang.reflect.Method.invoke(Native Method)
                      at com
.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                      at com
.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
                   
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.android.gms.auth.api.signin.GoogleSignInAccount.getIdToken()' on a null object reference
                      at com
.example.oseix.umc.LoginActivity.firebaseAuthWithGoogle(LoginActivity.java:286)
                      at com
.example.oseix.umc.LoginActivity.onActivityResult(LoginActivity.java:269)
                      at android
.app.Activity.dispatchActivityResult(Activity.java:6917)
                      at android
.app.ActivityThread.deliverResults(ActivityThread.java:4049)
                      at android
.app.ActivityThread.handleSendResult(ActivityThread.java:4096)
                      at android
.app.ActivityThread.-wrap20(ActivityThread.java)
                      at android
.app.ActivityThread$H.handleMessage(ActivityThread.java:1516)
                      at android
.os.Handler.dispatchMessage(Handler.java:102)
                      at android
.os.Looper.loop(Looper.java:154)
                      at android
.app.ActivityThread.main(ActivityThread.java:6077)
                      at java
.lang.reflect.Method.invoke(Native Method)
                      at com
.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                      at com
.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Application terminated.




Kato Richardson

unread,
Oct 11, 2016, 3:38:06 PM10/11/16
to Firebase Google Group
Hey Nana, great to meet you!

I don't see any code here calling getTokenId(), although that's clearly where the error is occurring. Can you provide a realistic repro that recreates the issue?

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-talk+unsubscribe@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/57107107-4010-46a5-98c0-8965cfd03bcd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

Kato Richardson | Developer Programs Eng | kato...@google.com | 775-235-8398

Kato Richardson

unread,
Oct 11, 2016, 3:43:36 PM10/11/16
to Firebase Google Group
Ah I see, because I was searching for getTokenId() instead of getIdToken(). Sorry about that.

So it looks like `result.getSignInAccount()` is returning null. I'm not an expert here, but looking at the API, that seems to indicate that the authentication attempt failed. So we can at least trace this back to the Google Auth API and the actual Google signin process.

I hope that helps!

☼, Kato

Ian Barber

unread,
Oct 12, 2016, 11:40:42 AM10/12/16
to Firebase Google Group
+1 to Kato, I suspect this is just sign in failing. You're checking whether the request was a sign in one, but you don't check the result. Take a look at https://firebase.google.com/docs/auth/android/google-signin#authenticate_with_firebase for a full snippet, but briefly you want to check  if (result.isSuccess()) after you retrieve it from getSignInResultFromIntent

To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.

To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/57107107-4010-46a5-98c0-8965cfd03bcd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

Kato Richardson | Developer Programs Eng | kato...@google.com | 775-235-8398

Reply all
Reply to author
Forward
0 new messages