Search code examples
androidgoogle-authentication

Android Google SignIn result is null


I am integrating Google sign-in for Android and I want the id_token back so I can authenticate with my server. I followed all the steps as in https://developers.google.com/identity/sign-in/android/backend-auth

If I request just requestEmail() then I am getting back the profile information but if I add .requestIdToken(getString(R.string.server_client_id)) then result.isSuccess() is false and I am not getting any account information back. Any ideas?

        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .requestIdToken(getString(R.string.server_client_id))
            .build();

onActivityResult:

            GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
            if (result.isSuccess()) {
                    GoogleSignInAccount acct = result.getSignInAccount();
                    String idToken = acct.getIdToken();
                    mIdTokenTextView.setText("ID Token: " + idToken);
                    // TODO(user): send token to server and validate server-side
            } else {
                    mIdTokenTextView.setText("ID Token: null");
            }

If I try to call any method on "result" then I get a Null pointer error...

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=9001, result=0, data=Intent { (has extras) }} to activity {com.test.LoginActivity}: java.lang.NullPointerException


Solution

  • SOLVED!

    So after 6 hrs of breaking my head, found the issue. Apparently when you create config.json using your SSH1 google automatically creates a Web OAuth2 key associated with this project. What I noticed was Google created 3 projects with the same name (one for firebase, one for Web and another not sure)

    So grab the Web client ID that is linked with the same Android project. Oh Google!