2
votes

I'm trying to add google sign in to my app using these instructions: https://developers.google.com/identity/sign-in/android/start-integrating

In my signInActivity I'm getting a null pointer exception on requestIdToken() in the command

mSignInOpt = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                     .requestIdToken(getString(R.string.default_web_client_id))
                     .build();

When I use this command without requestIdToken(), it is working perfectly and I'm getting user's name,email etc. in onActivityResult callback, but the token is null.

I made sure that both OAuth 2.0 client IDs in my web console match the ones I use in my app:

OAuth 2.0 client IDs in my web console

I also downloaded google-services.json that the new changes and copied it to my "mobile" folder of the project.

google-services.json I downloaded from firebase console

I've tried all the answers in the links below but nothing solved my problem.

Please Help!

Links:

  1. Google Sign-In requestIdToken returns null
  2. New Google sign in Android
  3. Error 12501 authenticating with google sign-in
4

4 Answers

4
votes

I figured it out.

mSignInOpt = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                     .requestIdToken(getString(R.string.default_web_client_id))
                     .build();

was outside of the OnCreate() method, meaning mSignInOpt is a global member for that activity and it is configured when activity is created.

For some reason .requestIdToken(getString(R.string.default_web_client_id)) cannot be called in that case ( without requestIdToken() it works fine ).

To fix the error, I moved those few lines of code into OnCreate() method and now it works!

1
votes

Sorry I don't have enough rep for a comment but can you do something like

mSignInOpt = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                 .requestIdToken(getString(R.string.default_web_client_id))
                 .requestEmail()
                 .build();

and it gives a valid response? or does it not work at all?

I'd try looking at this for more help.

1
votes
   googleSignInButton = findViewById(R.id.sign_in_button);
    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(getString(R.string.default_web_client_id))
            .requestEmail()
            .build();
0
votes

You missing this line in build.gradle apply plugin: 'com.google.gms.google-services'