使用 Firebase 身份验证时 Google 登录失败

时间:2021-01-26 15:21:06

标签: java android firebase google-signin googlesigninapi

我在按下 Google 登录按钮后选择帐户后收到以下错误。

com.google.android.gms.common.api.ApiException: 10: 
        at com.google.android.gms.common.internal.ApiExceptionUtil.fromStatus(com.google.android.gms:play-services-base@@17.1.0:4)
        at com.google.android.gms.auth.api.signin.GoogleSignIn.getSignedInAccountFromIntent(com.google.android.gms:play-services-auth@@19.0.0:9)
        at inspire2connect.inspire2connect.home.LoginActivity.onActivityResult(LoginActivity.java:125)
        at android.app.Activity.dispatchActivityResult(Activity.java:8310)
        at android.app.ActivityThread.deliverResults(ActivityThread.java:5008)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:5056)
        at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

我正在开发的应用已在 PlayStore 上架。我正在尝试为用户实施登录/注册方法,但我在这样做时遇到了问题。

Google Play 控制台上的凭据是说,SHA1:x(为了可读性和隐私),SHA256:x1

我在运行 signingReport 时获得的凭据是,SHA1:y 和 SHA256:y1

Google Console Cloud 上的凭据是 SHA1:x(与第一个相同)和 WebClientID:z

firebase 控制台上传的key是:x, x1, y, y1(全部上传)

我在调用 GoogleSignInOptions.Builder 时使用的客户端 ID 是 z

登录活动的代码是:

public class LoginActivity extends BaseActivity implements View.OnClickListener{

    private ConstraintLayout signInButton;
    private GoogleSignInClient mGoogleSignInClient;
    private  String TAG = "LoginActivity";
    private FirebaseAuth mAuth;
    private int RC_SIGN_IN = 1;
    private static final int MY_REQUEST_CODE = 2399;
    ConstraintLayout[] ll_but = new ConstraintLayout[10];
    ProgressDialog progressDialog;

    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(newBase);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStatusBarGradiant(this);
        setContentView(R.layout.activity_login);
        getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        progressDialog= new ProgressDialog(LoginActivity.this);
        progressDialog.setTitle("Please Wait..");
        progressDialog.setMessage("Loading");
        progressDialog.setCancelable(false);
        progressDialog.show();

        mAuth = FirebaseAuth.getInstance();

        ll_but[0] = findViewById(R.id.email_login_tile);
        ll_but[1] = findViewById(R.id.phone_login_tile);
        ll_but[2] = findViewById(R.id.google_login_tile);


        int[] btnToAdd = new int[]{0, 1, 2};

        for (int i = 0; i < btnToAdd.length; i++) {
            ll_but[btnToAdd[i]].setOnClickListener(this);
        }

        FirebaseUser user = mAuth.getCurrentUser();
        if (user != null) {
            progressDialog.dismiss();
            startActivity(new Intent(LoginActivity.this, homeActivity.class));
        }
        progressDialog.dismiss();

        signInButton = findViewById(R.id.google_login_tile);

        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestIdToken("1086619162320-po4o7git1lbllf86ukqfkvo2mh2ttsck.apps.googleusercontent.com")
                .requestEmail()
                .build();

        mGoogleSignInClient = GoogleSignIn.getClient(this, gso);


    }

    @Override
    public void onClick(View view) {
        Intent i = null;

        switch(view.getId()){
            case R.id.google_login_tile:
                signIn();
                break;

            case R.id.email_login_tile:
                i = new Intent(LoginActivity.this, EmailLogin.class);
                startActivity(i);
                break;

            case R.id.phone_login_tile:
                i = new Intent(LoginActivity.this, PhoneLogin.class);
                startActivity(i);
                break;
        }

    }

    private void signIn(){
        Intent signInIntent = mGoogleSignInClient.getSignInIntent();
        startActivityForResult(signInIntent, RC_SIGN_IN);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
        if (requestCode == RC_SIGN_IN) {
            Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
            try {
                // Google Sign In was successful, authenticate with Firebase
                GoogleSignInAccount account = task.getResult(ApiException.class);
                Log.d(TAG, "firebaseAuthWithGoogle:" + account.getId());
                firebaseAuthWithGoogle(account.getIdToken());
            } catch (ApiException e) {
                // Google Sign In failed, update UI appropriately
                Log.w(TAG, "Google sign in failed", e);
                // ...
            }
        }
    }

    private void firebaseAuthWithGoogle(String idToken) {
        AuthCredential credential = GoogleAuthProvider.getCredential(idToken, null);
        mAuth.signInWithCredential(credential)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            // Sign in success, update UI with the signed-in user's information
                            Log.d(TAG, "signInWithCredential:success");
                            FirebaseUser user = mAuth.getCurrentUser();
                            Intent i = new Intent(LoginActivity.this, homeActivity.class);
                            startActivity(i);
                        } else {
                            // If sign in fails, display a message to the user.
                            Log.w(TAG, "signInWithCredential:failure", task.getException());
                        }

                    }
                });
    }


   
}

这是我的 gradle 文件:

apply plugin: 'com.android.application'
apply plugin: 'com.google.firebase.crashlytics'

android {
    compileSdkVersion 29

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    defaultConfig {
        applicationId "inspire2connect.inspire2connect"
        minSdkVersion 21
        targetSdkVersion 29
        versionCode 24
        versionName "4.1"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            debuggable false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    def room_version = "2.2.5"
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    // Android & UI
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
//    implementation 'org.jetbrains:annotations-java5:15.0'

    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.google.android.material:material:1.1.0'
    implementation 'com.google.android.play:core:1.7.3'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
    implementation 'androidx.mediarouter:mediarouter:1.1.0'
    implementation 'androidx.recyclerview:recyclerview:1.1.0'
    implementation "androidx.viewpager:viewpager:1.0.0"
    implementation 'android.arch.work:work-runtime:1.0.1'
    implementation 'androidx.preference:preference:1.1.1'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.legacy:legacy-support-v13:1.0.0'
    implementation 'androidx.cardview:cardview:1.0.0'
    implementation 'androidx.vectordrawable:vectordrawable-animated:1.1.0'
    implementation 'com.google.guava:guava:27.0.1-android'
    implementation 'com.android.volley:volley:1.1.1'

    // Firebase
    implementation platform('com.google.firebase:firebase-bom:26.3.0')
    implementation 'com.google.firebase:firebase-database:19.3.0'
    implementation 'com.firebaseui:firebase-ui-auth:6.2.0'
    implementation 'com.google.android.gms:play-services-auth:19.0.0'
    implementation 'com.google.android.gms:play-services-basement:17.5.0'
    implementation 'com.google.firebase:firebase-auth:19.3.1'
    implementation 'com.google.firebase:firebase-storage:19.1.1'
    implementation 'com.google.firebase:firebase-inappmessaging-display:19.0.7'
    implementation 'com.google.firebase:firebase-analytics:17.4.3'
    implementation 'com.google.firebase:firebase-messaging:20.2.0'
    implementation 'com.google.firebase:firebase-core:17.4.3'
    implementation 'com.google.firebase:firebase-iid:20.2.0'
    implementation 'com.google.firebase:firebase-crashlytics:17.0.1'

    // Room
    implementation "androidx.room:room-runtime:$room_version"
    annotationProcessor "androidx.room:room-compiler:$room_version"
    testImplementation "androidx.room:room-testing:$room_version"

    // Third Party
    implementation 'io.branch.sdk.android:library:5.+'
    implementation 'com.squareup.picasso:picasso:2.71828'
    implementation 'com.github.lecho:hellocharts-library:1.5.8@aar'
    implementation 'com.llollox:androidtoggleswitch:2.0.1'
    implementation 'com.sefford:circular-progress-drawable:1.31@aar'
    implementation 'androidx.gridlayout:gridlayout:1.0.0'
    implementation 'com.jakewharton:butterknife:10.2.1'
    annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.1'
    implementation 'in.srain.cube:grid-view-with-header-footer:1.0.12'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
    implementation 'com.google.android.gms:play-services-maps:17.0.0'
    implementation 'com.google.firebase:firebase-analytics:17.2.2'
    implementation 'com.squareup.okhttp3:okhttp:4.4.0'
    implementation 'com.google.android.gms:play-services-location:17.0.0'
    implementation 'com.shobhitpuri.custombuttons:google-signin:1.1.0'
//    implementation files('libs/YouTubeAndroidPlayerApi.jar')

}

apply plugin: 'com.google.gms.google-services'

我哪里出错了?

0 个答案:

没有答案