GoogleSignInResult returns null GoogleSignInAccount in production

Viewed 236

I am following this to integrate google authentication in my app. It is working fine in debug apks, but when released, it is failing every time.

I have registered both debug and relese SHA-1 key on cloud platform.

code snippet for reference-

private void someMethid(){        
String languageURL = "https://www.googleapis.com/auth/profile.language.read";
String genderURL = "https://www.googleapis.com/auth/user.gender.read";
String birthdayURL = "https://www.googleapis.com/auth/user.birthday.read";
GoogleSignInOptions option = new 
GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .requestScopes(
                    new Scope(languageURL),
                    new Scope(genderURL),
                    new Scope(birthdayURL)
            )
            .build();
    client = GoogleSignIn.getClient(this, option);
    signIn();
}

private void signIn() {

    Intent signInIntent = client.getSignInIntent();
    startActivityForResult(signInIntent, RC_SIGN_IN);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RC_SIGN_IN) {

        Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
        handleSignInResult(task);
    }
}

private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
    GoogleSignInAccount acct = null;
    try {
        acct = completedTask.getResult(ApiException.class);
        if (acct != null) {
            fetchData(acct);
        }
        else {
            showDialogMessage("Email verification failed", "failed to get Google account information", false);
            client.revokeAccess();
            client.signOut();
        }
    } catch (ApiException e) {
        e.printStackTrace();
        showDialogMessage("Email verification failed", "failed to get Google account information", false);
        client.revokeAccess();
        client.signOut();
    }

The SignIn prompt opens, does ask for google account, but does not ask for permissions of the requested scopes and retuns null for GoogleSignInAccount.

2 Answers

When you say When released, do you mean when you install the app from Google Play? If you're using "Play App Signing", you need to add the signing key from the Google Play Console, not from your repository/ keystore. This is because Google Play signs the app on your behalf.

Diagnosing your problem: It looks like the app was signed with a different key to the one the authentication service is expecting, so the service rejects it. It was resigned with you knowing about it.

https://support.google.com/googleplay/android-developer/answer/9842756?hl=en

If that doesn't work, please give more detail :). Let me know if you need more help.

After Uploading to play store, Play store generate its own SH1 which we have to replace in second key in google console.

Go to google console>app signing> copy SH1 generate by console. Add that in google sign in console replace it from 2nd key.

Related