Google登录失败,Api异常12500

时间:2019-02-04 09:27:43

标签: android android-intent

我已将SHA1添加到我的firebase项目中,还添加了SHA256。我仍然不断收到以下错误。 我也启用了Google登录方法。 请我没有选择权。

我的build.gradle

implementation 'com.google.firebase:firebase-core:16.0.6'
implementation 'com.google.firebase:firebase-auth:16.1.0'
implementation 'com.google.android.gms:play-services-auth:16.0.1'

错误报告:

W/SigninActivity: Google sign in failed
com.google.android.gms.common.api.ApiException: 12500: 
    at com.google.android.gms.common.internal.ApiExceptionUtil.fromStatus(Unknown Source)
    at com.google.android.gms.auth.api.signin.GoogleSignIn.getSignedInAccountFromIntent(Unknown Source)
    at org.aac.detailor.SigninActivity.onActivityResult(SigninActivity.java:83)
    at android.app.Activity.dispatchActivityResult(Activity.java:6294)
    at android.app.ActivityThread.deliverResults(ActivityThread.java:3829)
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3876)
    at android.app.ActivityThread.access$1300(ActivityThread.java:178)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1519)
    at android.os.Handler.dispatchMessage(Handler.java:111)
    at android.os.Looper.loop(Looper.java:194)
    at android.app.ActivityThread.main(ActivityThread.java:5631)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)

这是我的登录活动,我根据Firebase身份验证中的文档执行了“身份验证”步骤。

我的登录活动:

public class SigninActivity extends AppCompatActivity {

FirebaseAuth auth;
GoogleSignInClient mGoogleSignInClient;

SignInButton googleSignIn;
FirebaseAuth.AuthStateListener authStateListener;

private static final int RC_SIGN_IN = 111;
private static final String TAG = "SigninActivity";

public static String username = "org.aac.trial.username";
public static String email = "org.aac.trial.email";

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

    auth = FirebaseAuth.getInstance();

    auth = FirebaseAuth.getInstance();
    authStateListener = firebaseAuth -> {
        if (firebaseAuth.getCurrentUser() != null) {
            Toast.makeText(SigninActivity.this, "Welcome "+
                    firebaseAuth.getCurrentUser().getDisplayName(), Toast.LENGTH_LONG).show();
Intent intent = new Intent(SigninActivity.this, MainActivity.class);
            startActivity(intent);
            return;
        }
    };
    auth.addAuthStateListener(authStateListener);

    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken("891757993158-tle1db5q6ems263o723uejcfcn486ush.apps.googleusercontent.com")
            .requestEmail()
            .build();
    mGoogleSignInClient = GoogleSignIn.getClient(this, gso);

    googleSignIn = findViewById(R.id.signInGoogle);
    googleSignIn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            signIn();
        }
    });
}

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

@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", e);
            Toast.makeText(SigninActivity.this, "Google Sign In Failed \n"+e, Toast.LENGTH_LONG).show();
        }
    }
}

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

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

                    }

                    // ...
                }
            });
}

private void updateUI(FirebaseUser user) {
    Intent intent = new Intent(SigninActivity.this, MainActivity.class);
    Bundle bundle = new Bundle();
    bundle.putString(username, user.getDisplayName());
    bundle.putString(email, user.getEmail());
    intent.putExtras(bundle);
    startActivity(intent);
}

}

2 个答案:

答案 0 :(得分:-1)

您好kpa_robor我曾经有一个类似的问题,当我尝试使用自己的google帐户登录时,该应用程序不允许我登录。请检查您的应用程序是否已连接到Firebase,请参见下面的图片。

我希望这可以解决您的问题!

enter image description here

答案 1 :(得分:-1)

  1. 查看是否已将SHA-1,SHA-256指纹设置为您创建的firebase应用程序。添加相关指纹后,下载google-services.json并再次将其替换为您的项目。

SHA-1 and SHA-256 certificate fingerprint

  1. 确认后,您已在Firebase控制台中授予了相关的登录方法。我已启用Google登录方法

enter image description here