Google sign-in fragment returning RESULT_CANCELED

acn*_*cn4 5 android firebase google-signin

I am trying to integrate Google sign-in into my android app using Firebase, but I am running into issues. I am following the tutorial here, and I believe that I have followed it to the word. I added Firebase to my app, added my SHA-1 fingerprint, enabled Google sign-in, and added the dependencies to my grade files. Then, I copied the code in the github project linked to in the tutorial. However, when I run the application, and my code is below, I get an error when the sign-in fragment returns, and the result code is RESULT_CANCELED. This is the error output:

W/GoogleSignInActivity: Google sign in failed, resultCode: 0
                    com.google.android.gms.common.api.ApiException: 10: 
                        at com.google.android.gms.common.internal.zzb.zzy(Unknown Source:14)
                        at com.google.android.gms.auth.api.signin.GoogleSignIn.getSignedInAccountFromIntent(Unknown Source:37)
                        at com.example.root.firebasesignin.LoginActivity.onActivityResult(LoginActivity.java:63)
                        at android.app.Activity.dispatchActivityResult(Activity.java:7267)
                        at android.app.ActivityThread.deliverResults(ActivityThread.java:4524)
                        at android.app.ActivityThread.handleSendResult(ActivityThread.java:4571)
                        at android.app.ActivityThread.-wrap19(Unknown Source:0)
                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1744)
                        at android.os.Handler.dispatchMessage(Handler.java:105)
                        at android.os.Looper.loop(Looper.java:164)
                        at android.app.ActivityThread.main(ActivityThread.java:6809)
                        at java.lang.reflect.Method.invoke(Native Method)
                        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Run Code Online (Sandbox Code Playgroud)

When I created the SHA-1 fingerprint, I did have to remake the debug keystore at ~/.android/debug.keystore using the command

keytool -genkey -v -keystore ~/.android/debug.keystore -storepass android -alias androiddebugkey -keypass android -dname "CN=Android Debug,O=Android,C=US"
Run Code Online (Sandbox Code Playgroud)

and then I converted the keystore to pkcs12 using the command

keytool -importkeystore -srckeystore ~/.android/debug.keystore -destkeystore ~/.android/debug.keystore -deststoretype pkcs12
Run Code Online (Sandbox Code Playgroud)

I have never used Firebase or Google sign-in for authentication, so I am very lost. I think the keystore might be the problem, and when I look in File > Project Structure > Signings nothing is shown in the left panel, and in File > Project Structure > Build Types the Signing Config box is empty. Again, I am very new to Firebase and Google authentication, so please excuse me if I'm forgetting something simple. Thank you in advance for your help.

This is the code for my main activity. The layout is simply a Google sign-in button and the action bar.

package com.example.root.firebasesignin;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInClient;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.common.api.ApiException;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthCredential;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.auth.GoogleAuthProvider;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "GoogleSignInActivity";
    private static final int RC_SIGN_IN = 9001;

    private FirebaseAuth mAuth;
    private GoogleSignInClient mGoogleSignInClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        // Button listeners
        findViewById(R.id.sign_in_button).setOnClickListener(this);

        // Configure Google Sign In
        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestIdToken(getString(R.string.default_web_client_id))
                .requestEmail()
                .build();

        mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
        mAuth = FirebaseAuth.getInstance();
    }

    @Override
    public void onStart() {
        super.onStart();
        // Check if user is signed in (non-null) and update UI accordingly.
        FirebaseUser currentUser = mAuth.getCurrentUser();
        updateUI(currentUser);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, 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);
                firebaseAuthWithGoogle(account);
            } catch (ApiException e) {
                // Google Sign In failed, update UI appropriately
                Log.w(TAG, "Google sign in failed, resultCode: " + resultCode, e);
                updateUI(null);
            }
        }
    }

    private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
        Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());

        AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), 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();
                            updateUI(user);
                        } else {
                            // If sign in fails, display a message to the user.
                            Log.w(TAG, "signInWithCredential:failure", task.getException());
                            Toast.makeText(LoginActivity.this, "Authentiation failed", Toast.LENGTH_LONG).show();
                            updateUI(null);
                        }
                    }
                });
    }

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

    private void updateUI(FirebaseUser user) {
        if (user != null)
            findViewById(R.id.sign_in_button).setVisibility(View.GONE);
        else
            findViewById(R.id.sign_in_button).setVisibility(View.VISIBLE);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.sign_in_button)
            signIn();
    }
}
Run Code Online (Sandbox Code Playgroud)

Cha*_*Wei 4

com.google.android.gms.common.api.ApiException: 10由以下块引用解释。

开发者错误 应用程序配置错误。此错误不可恢复,将被视为致命错误。开发人员应在此之后查看日志以确定更多可操作的信息。

这意味着您的 SHA-1 指纹或 OAuth 2.0 客户端 ID 可能有问题。自从您遵循本教程以来,我怀疑您的 SHA-1 配置有什么问题。剩下的唯一一件事就是获取正确的客户端 ID。您首先转到https://console.cloud.google.com/apis/credentials

  1. 查找与Android相关且位于OAuth 2.0 client IDs下的任何内容。
  2. 将部分代码替换为以下内容:

    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(webClientId)
            .requestEmail()
            .build();
    
    Run Code Online (Sandbox Code Playgroud)

该变量webClientId保存您之前从https://console.cloud.google.com/apis/credentials获取的客户端 ID。然后,就这样吧。

  • 我找到了 Android 的 OAuth 2.0 客户端 ID,它是由 Google 服务自动创建的,并替换了该值,但它给出了相同的错误。我还尝试了由 Google Service 自动创建的网络客户端,它给出了相同的结果。 (2认同)