首页 > 解决方案 > GoogleSignInResult 在生产中返回 null GoogleSignInAccount

问题描述

我正在关注将谷歌身份验证集成到我的应用程序中。它在调试 apk 中运行良好,但在发布时,每次都失败。

我已经在云平台上注册了 debug 和 relese SHA-1 密钥。

供参考的代码片段-

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();
    }

登录提示打开,确实要求提供 google 帐户,但不要求提供所请求范围的权限,并为 GoogleSignInAccount 返回 null。

标签: androidgoogle-signin

解决方案


当您说When released时,您的意思是当您从 Google Play 安装应用程序时?如果您使用的是“Play App Signing”,则需要从 Google Play 控制台添加签名密钥,而不是从您的存储库/密钥库。这是因为 Google Play 代表您签署了该应用程序。

诊断您的问题:看起来应用程序是使用与身份验证服务所期望的密钥不同的密钥签名的,因此服务拒绝了它。它是在你知道的情况下辞职的。

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

如果这不起作用,请提供更多详细信息:)。如果您需要更多帮助,请告诉我。


推荐阅读