我使用了Stackoverflow,并尝试了建议的答案,但都不起作用。我正在尝试使用fireless和谷歌对用户进行身份验证,我的代码运行良好。但在这种方法中,我总是得到toast消息身份验证失败:
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
auth.signInWithCredential(credential)
.addOnCompleteListener(LoginActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
// 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()) {
Toast.makeText(LoginActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
// ...
}
});
}
我相信这与我的google-services.json和我的googleneneneba API证书有关。我想知道是否有人能给我一些关于如何解决这个问题的明确步骤。这就是我所做的,试图让签约过程发挥作用。
- 将我的应用程序与firebase链接,并将JSON正确添加到项目中
- 添加了所有构建渐变依赖项
- 允许Google从firebase auth选项卡登录
- 去谷歌API证书网站(这里),复制并存储在我的项目中的strings.xml中
- 返回凭据页面,为Android创建了另一个Auth令牌,并将其键入终端以获取ShA1密钥:
keystore-list-vrt-别名androiddebugkey-keystore~/.android/debug.
密码为安卓
然后我在创建android Auth令牌时输入了这个密钥。
我在onCreate 中的signin类中也有所有这些代码
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.server_client_id))
.requestEmail()
.build();
// Build a GoogleApiClient with access to the Google Sign-In API and the
// options specified by gso.
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
所有这些,以及监听器和启动和停止方法
private void signIn() {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
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) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
if (result.isSuccess()) {
Toast.makeText(LoginActivity.this, "Passed google login", Toast.LENGTH_SHORT).show();
// Google Sign In was successful, authenticate with Firebase
GoogleSignInAccount account = result.getSignInAccount();
firebaseAuthWithGoogle(account);
}
}
}
/*****************************************************************************************************/
/*get an ID token from the GoogleSignInAccount object, exchange it for a Firebase credential,
*and authenticate with Firebase using the Firebase credential
*/
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
auth.signInWithCredential(credential)
.addOnCompleteListener(LoginActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
// 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()) {
Toast.makeText(LoginActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
// ...
}
});
}
我在这件事上已经坚持了很长时间,我们非常感谢你的帮助。我不清楚我的问题是什么,我只是猜测。如果需要任何进一步的信息,请让我知道,我会更新这篇文章。
感谢
这与其说是一个答案,不如说是一组建议。
更新您的完成侦听器以记录失败的异常:
if (!task.isSuccessful()) {
Log.w(TAG, "signInWithCredential", task.getException());
Toast.makeText(GoogleSignInActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
运行时,查看logcat
输出以查看异常和其他与身份验证处理相关的日志消息。
我想知道你发布的这个步骤以及与你的项目的SHA1指纹相关的步骤顺序:
步骤4。去谷歌API认证网站。。。
我有几个项目的授权工作,包括AuthQuickStart,并且不记得需要使用API证书网站。SHA1密钥应在Firebase控制台的项目设置页面中输入。您应该在下载google-service.json
文件之前执行,以便将其包含在文件中。您可以通过查看certificate_hash
的值来查看您正在使用的文件是否具有正确的指纹
"oauth_client": [
{
"client_id": "<long ID string here>",
"client_type": 1,
"android_info": {
"package_name": "com.google.firebase.quickstart.auth",
"certificate_hash": "<your SHA1 fingerprint here>"
}
},