关于这个模糊的错误代码已经有几个问题,但没有人为我解决了这个问题,所以我会再试一次。首先,这是用于登录的样板代码。
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.requestServerAuthCode("web app client ID copied from Dev API Console", false)
.build();
mGoogleApiClient = new GoogleApiClient.Builder(this).enableAutoManage(this, this).addApi(Auth
.GOOGLE_SIGN_IN_API, gso).build();
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
使用此代码,我会得到一个对话框,可以选择适当的用户帐户。好极了!但是,然后我在onActivityResult中获取了一个已取消的代码,因此我检查了logcat并注意了标题中的错误消息。然后我搜索了这个问题的其他人,但是我无法弄清楚问题是什么。
所以让我们从其他帖子中查看常见问题。
对于可搜索性,在标题中的logcat错误消息之后,我得到以下logcat打印。
你有错误的OAuth2相关配置,请检查。详细 错误:INVALID_AUDIENCE
答案 0 :(得分:2)
您的网络客户端ID必须与Firebase在同一用户帐户中生成,而不是您的Play商店帐户。就我而言,这两个帐户是分开的,Google会抛出此错误,而不是正确链接信息。我的Firebase帐户已链接到我的Play商店帐户,但这显然并不意味着将共享该网络客户端ID。我尝试了其他一些解决方案,例如链接API Manager帐户,但这些解决方案也没有用。您必须在Firebase在与您的Firebase帐户相同的API Manager帐户中使用的同一项目内创建Web客户端ID。
答案 1 :(得分:0)
问题在于您的client_id,用于google auth进程。从当前项目中删除该文件。然后添加新实例。这次要特别注意id。使用android-studio生成firebase的设置。 您必须拥有一个包含多个客户端ID的项目 一个是你的Android应用程序,与SHA。最安全的赌注是在谷歌控制台中删除你当前的项目。然后重新创建它。然后在android studio中go tools => firebase然后选择要集成的设置。我不建议手动添加。
答案 2 :(得分:0)
实施您的活动
GoogleApiClient.OnConnectionFailedListener
设置这些私有变量
private GoogleApiClient mGoogleApiClient;
private FirebaseAuth mFirebaseAuth;
private FirebaseAuth mFirebaseAuth;
private static final int RC_SIGN_IN = 9001;
配置Google登录
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(SignInActivity.this.getResources().getString(R.string.default_web_client_id))
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
初始化FirebaseAuth
mFirebaseAuth = FirebaseAuth.getInstance();
mFirebaseUser = mFirebaseAuth.getCurrentUser();
if (mFirebaseUser == null) {
// Not signed in, launch the Sign In activity
} else {
// Already signed in, launch the Home activity
//You can use mFirebaseUser.getDisplayName() / getUid() / getEmail() /getPhotoUrl()
}
登录方法
private void signIn() {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
}
<强> onActivityResult()强>
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.e("Data",data.getExtras().toString());
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
Log.e("Result","status "+result.getStatus()+"Success"+result.isSuccess()+" "+result.getSignInAccount());
if (result.isSuccess()) {
// Google Sign In was successful, authenticate with Firebase
GoogleSignInAccount account = result.getSignInAccount();
firebaseAuthWithGoogle(account);
} else {
// Google Sign In failed
Log.e(TAG, "Google Sign In failed.");
}
}
}
<强> firebaseAuthWithGoogle 强>
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
mFirebaseAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d(TAG, "signInWithCredential:onComplete:" + task.isSuccessful());
// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
Log.w(TAG, "signInWithCredential", task.getException());
Toast.makeText(SignInActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(SignInActivity.this, "Authentication Successful.",
Toast.LENGTH_SHORT).show();
}
}
});
}
<强> onConnectionFailed()强>
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
// An unresolvable error has occurred and Google APIs (including Sign-In) will not
// be available.
Log.d(TAG, "onConnectionFailed:" + connectionResult);
Toast.makeText(this, "Google Play Services error.", Toast.LENGTH_SHORT).show();
}
退出方法
private void signOut() {
Auth.GoogleSignInApi.signOut(mGoogleApiClient).
setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(@NonNull Status status) {
}
});
mFirebaseAuth.signOut();
}
将日志保存在每个点。 如果你按照你所做的列表正确地跟踪了所有的点,它应该可以工作。