Android Google SignIn result is null

扶醉桌前 提交于 2019-12-08 07:54:50

问题


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


回答1:


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!




回答2:


I have found after researching about this issue and found that result is throwing null pointer Exception due to android:launchMode="singleInstance" in activity manifest.

So make sure you Change launchmode to 'singleTop' or just dont give any launchmode for the your activity for e.g: signinActivity;



来源:https://stackoverflow.com/questions/45208827/android-google-signin-result-is-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!