2

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
Community
  • 1
  • 1
MiSo
  • 443
  • 1
  • 7
  • 16

4 Answers4

4

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!

Omar Dhanish
  • 885
  • 7
  • 18
MiSo
  • 443
  • 1
  • 7
  • 16
1

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.

Imran Ali
  • 104
  • 13
  • I tried that. I get the same exception on .requestIdToken(getString(R.string.default_web_client_id)) – MiSo Aug 12 '16 at 06:08
  • GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) .requestProfile() .requestEmail() .requestIdToken(getString(R.string.default_web_client_id)) .build(); This is how i added and it works fine – R.Anjali Feb 07 '19 at 07:33
1
   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

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

Vikash Sharma
  • 539
  • 8
  • 13