繁体   English   中英

返回 RESULT_CANCELED 的 Google 登录片段

[英]Google sign-in fragment returning RESULT_CANCELED

我正在尝试使用 Firebase 将 Google 登录集成到我的 android 应用程序中,但我遇到了问题。 我正在关注这里的教程,并且我相信我已经按照它来做。 我将 Firebase 添加到我的应用程序中,添加了我的 SHA-1 指纹,启用了 Google 登录,并将依赖项添加到我的成绩文件中。 然后,我复制了教程中链接到的 github 项目中的代码。 但是,当我运行应用程序时,我的代码如下,登录片段返回时出现错误,结果代码为RESULT_CANCELED 这是错误 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)

当我创建 SHA-1 指纹时,我确实必须使用命令在~/.android/debug.keystore重新制作调试密钥库

keytool -genkey -v -keystore ~/.android/debug.keystore -storepass android -alias androiddebugkey -keypass android -dname "CN=Android Debug,O=Android,C=US"

然后我使用命令将密钥库转换为 pkcs12

keytool -importkeystore -srckeystore ~/.android/debug.keystore -destkeystore ~/.android/debug.keystore -deststoretype pkcs12

我从来没有使用过 Firebase 或谷歌登录进行身份验证,所以我很迷茫。 我认为密钥库可能是问题所在,当我查看File > Project Structure > Signings时,左侧面板中没有显示任何内容,并且在File > Project Structure > Build Types中,Signing Config 框为空。 同样,我对 Firebase 和 Google 身份验证非常陌生,所以如果我忘记了一些简单的事情,请原谅。 预先感谢您的帮助。

这是我的主要活动的代码。 布局只是一个 Google 登录按钮和操作栏。

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

我正在尝试使用Firebase将Google登录集成到我的android应用中,但是遇到了问题。 我在这里关注本教程,并且相信我已经遵循了这个词。 我将Firebase添加到我的应用中,添加了SHA-1指纹,启用了Google登录,并将依赖项添加到了成绩文件中。 然后,我将代码复制到了本教程中链接到的github项目中。 但是,当我运行应用程序且代码在下面时,登录片段返回时出现错误,结果代码为RESULT_CANCELED 这是错误输出:

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)

创建SHA-1指纹时,确实必须使用以下命令在~/.android/debug.keystore重新制作调试密钥库。

keytool -genkey -v -keystore ~/.android/debug.keystore -storepass android -alias androiddebugkey -keypass android -dname "CN=Android Debug,O=Android,C=US"

然后使用以下命令将密钥库转换为pkcs12

keytool -importkeystore -srckeystore ~/.android/debug.keystore -destkeystore ~/.android/debug.keystore -deststoretype pkcs12

我从未使用过Firebase或Google登录进行身份验证,所以我很迷路。 我认为密钥库可能是问题所在,当我在“ File > Project Structure > Signings “签名”中File > Project Structure > Build Types ,左面板中没有显示任何内容,在“ File > Project Structure > Build Types ,“签名配置”框为空。 同样,我对Firebase和Google身份验证还很陌生,因此,如果我忘记了一些简单的内容,请原谅。 预先感谢您的帮助。

这是我主要活动的代码。 该布局只是一个Google登录按钮和操作栏。

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

我正在尝试使用Firebase将Google登录集成到我的android应用中,但是遇到了问题。 我在这里关注本教程,并且相信我已经遵循了这个词。 我将Firebase添加到我的应用中,添加了SHA-1指纹,启用了Google登录,并将依赖项添加到了成绩文件中。 然后,我将代码复制到了本教程中链接到的github项目中。 但是,当我运行应用程序且代码在下面时,登录片段返回时出现错误,结果代码为RESULT_CANCELED 这是错误输出:

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)

创建SHA-1指纹时,确实必须使用以下命令在~/.android/debug.keystore重新制作调试密钥库。

keytool -genkey -v -keystore ~/.android/debug.keystore -storepass android -alias androiddebugkey -keypass android -dname "CN=Android Debug,O=Android,C=US"

然后使用以下命令将密钥库转换为pkcs12

keytool -importkeystore -srckeystore ~/.android/debug.keystore -destkeystore ~/.android/debug.keystore -deststoretype pkcs12

我从未使用过Firebase或Google登录进行身份验证,所以我很迷路。 我认为密钥库可能是问题所在,当我在“ File > Project Structure > Signings “签名”中File > Project Structure > Build Types ,左面板中没有显示任何内容,在“ File > Project Structure > Build Types ,“签名配置”框为空。 同样,我对Firebase和Google身份验证还很陌生,因此,如果我忘记了一些简单的内容,请原谅。 预先感谢您的帮助。

这是我主要活动的代码。 该布局只是一个Google登录按钮和操作栏。

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

我正在尝试使用Firebase将Google登录集成到我的android应用中,但是遇到了问题。 我在这里关注本教程,并且相信我已经遵循了这个词。 我将Firebase添加到我的应用中,添加了SHA-1指纹,启用了Google登录,并将依赖项添加到了成绩文件中。 然后,我将代码复制到了本教程中链接到的github项目中。 但是,当我运行应用程序且代码在下面时,登录片段返回时出现错误,结果代码为RESULT_CANCELED 这是错误输出:

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)

创建SHA-1指纹时,确实必须使用以下命令在~/.android/debug.keystore重新制作调试密钥库。

keytool -genkey -v -keystore ~/.android/debug.keystore -storepass android -alias androiddebugkey -keypass android -dname "CN=Android Debug,O=Android,C=US"

然后使用以下命令将密钥库转换为pkcs12

keytool -importkeystore -srckeystore ~/.android/debug.keystore -destkeystore ~/.android/debug.keystore -deststoretype pkcs12

我从未使用过Firebase或Google登录进行身份验证,所以我很迷路。 我认为密钥库可能是问题所在,当我在“ File > Project Structure > Signings “签名”中File > Project Structure > Build Types ,左面板中没有显示任何内容,在“ File > Project Structure > Build Types ,“签名配置”框为空。 同样,我对Firebase和Google身份验证还很陌生,因此,如果我忘记了一些简单的内容,请原谅。 预先感谢您的帮助。

这是我主要活动的代码。 该布局只是一个Google登录按钮和操作栏。

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

我正在尝试使用Firebase将Google登录集成到我的android应用中,但是遇到了问题。 我在这里关注本教程,并且相信我已经遵循了这个词。 我将Firebase添加到我的应用中,添加了SHA-1指纹,启用了Google登录,并将依赖项添加到了成绩文件中。 然后,我将代码复制到了本教程中链接到的github项目中。 但是,当我运行应用程序且代码在下面时,登录片段返回时出现错误,结果代码为RESULT_CANCELED 这是错误输出:

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)

创建SHA-1指纹时,确实必须使用以下命令在~/.android/debug.keystore重新制作调试密钥库。

keytool -genkey -v -keystore ~/.android/debug.keystore -storepass android -alias androiddebugkey -keypass android -dname "CN=Android Debug,O=Android,C=US"

然后使用以下命令将密钥库转换为pkcs12

keytool -importkeystore -srckeystore ~/.android/debug.keystore -destkeystore ~/.android/debug.keystore -deststoretype pkcs12

我从未使用过Firebase或Google登录进行身份验证,所以我很迷路。 我认为密钥库可能是问题所在,当我在“ File > Project Structure > Signings “签名”中File > Project Structure > Build Types ,左面板中没有显示任何内容,在“ File > Project Structure > Build Types ,“签名配置”框为空。 同样,我对Firebase和Google身份验证还很陌生,因此,如果我忘记了一些简单的内容,请原谅。 预先感谢您的帮助。

这是我主要活动的代码。 该布局只是一个Google登录按钮和操作栏。

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

我使用以下代码遇到了类似的情况:

private static final int PLAY_LOGIN = 1979;

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).build();
GoogleSignInClient client = GoogleSignIn.getClient(activity, gso);
activity.startActivityForResult(client.getSignInIntent(), PLAY_LOGIN);

并尝试在该方法中获取登录结果:

@Override
public boolean onActivityResult(@NonNull Context context, int requestCode, int resultCode, Intent data) {
    if (requestCode == PLAY_LOGIN) {
        if (resultCode == Activity.RESULT_OK) {
            Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
            // process successful login data here...
        } else {
            Toast.makeText(context, "login error!", Toast.LENGTH_LONG).show();
        }

        return true;
    }
    return false;
}

有时(并不总是可重现)返回的代码不是Activity.RESULT_OK ,而是Activity.RESULT_CANCELED - 即使我总是接受 Google 对话框中的登录选项...

所以我的代码显示了带有“登录错误”的Toast 这甚至导致我的应用没有通过 Google 审核流程。

为了调查这个问题,我将调试器断点放置在显示Toast的行处,然后继续重新安装我的应用程序并接受 Google 登录对话框。

然后有时会遇到断点,并且Intent具有以下mExtras值:

捆绑[{googleSignInStatus=Status{statusCode=未知状态码:12502,分辨率=null}}]

根据GoogleSignInStatusCodes上的 Google 文档,代码 GoogleSignInStatusCodes.SIGN_IN_CURRENTLY_IN_PROGRESS=12502 表示另一个登录已经在进行中:

文档截图

因此,尽管在 Firebase 控制台中正确配置了所有内容,但我的回答是提醒任何遇到此问题的人。

您可能希望在代码中处理这种情况并向用户显示不同的消息,可能不是错误,而只是警告。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM