Java 谷歌登录认证不起作用
我使用了Stackoverflow并尝试了建议的答案,但没有一个有效。我正在尝试用firebease和google对用户进行身份验证,我的代码运行良好。但在这种方法中,我总是得到toast消息身份验证失败:Java 谷歌登录认证不起作用,java,android,firebase,google-api,firebase-authentication,Java,Android,Firebase,Google Api,Firebase Authentication,我使用了Stackoverflow并尝试了建议的答案,但没有一个有效。我正在尝试用firebease和google对用户进行身份验证,我的代码运行良好。但在这种方法中,我总是得到toast消息身份验证失败: private void firebaseAuthWithGoogle(GoogleSignInAccount acct) { AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken()
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();
}
// ...
}
});
}
所有这些,还有listener、onstart和stop方法
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();
}
// ...
}
});
}
private void signIn(){
意向符号=Auth.googlesignianpi.getsignient(mgoogleapclient);
startActivityForResult(签名、注册);
}
@凌驾
ActivityResult上的公共void(int请求代码、int结果代码、意图数据){
super.onActivityResult(请求代码、结果代码、数据);
//从GoogleSignInApi.getsigninent(…)启动Intent返回的结果;
if(requestCode==RC\u登录){
GoogleSignInResult结果=Auth.GoogleSignInApi.getSignInResultFromIntent(数据);
if(result.issucess()){
Toast.makeText(LoginActivity.this,“通过谷歌登录”,Toast.LENGTH_SHORT.show();
//Google登录成功,通过Firebase验证
GoogleSignInAccount account=result.getSignInAccount();
firebaseAuthWithGoogle(账户);
}
}
}
/*****************************************************************************************************/
/*从GoogleSignInAccount对象获取ID令牌,将其交换为Firebase凭据,
*并使用Firebase凭据向Firebase进行身份验证
*/
私有void firebaseAuthWithGoogle(谷歌签名帐户){
AuthCredential credential=GoogleAuthProvider.getCredential(acct.getIdToken(),null);
使用凭证(凭证)进行身份验证登录
.addOnCompleteListener(LoginActivity.this,新的OnCompleteListener(){
@凌驾
未完成的公共void(@NonNull任务){
//如果登录失败,则向用户显示消息。如果登录成功
//将通知身份验证状态侦听器,并使用逻辑来处理
//可以在侦听器中处理已登录用户。
如果(!task.issusccessful()){
Toast.makeText(LoginActivity.this,“身份验证失败”,
吐司。长度(短)。show();
}
// ...
}
});
}
我已经在这个问题上坚持了很长时间,非常感谢您的帮助。我不清楚我的问题是什么,我只是猜测而已。如果需要任何进一步的信息,请让我知道,我会更新这篇文章
谢谢这与其说是答案,不如说是一系列建议 更新完成侦听器以记录失败的异常:
if (!task.isSuccessful()) {
Log.w(TAG, "signInWithCredential", task.getException());
Toast.makeText(GoogleSignInActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
运行时,查看logcat
输出以查看异常和与身份验证处理相关的其他日志消息
我想知道您发布的这一步骤以及与您的项目的SHA1指纹相关的步骤顺序:
第四步。转到谷歌API认证网站
我已经在几个项目中工作过auth,包括,但我不记得需要使用API凭据网站。SHA1密钥应在项目的“设置”页面中输入。您应该在下载googleservice.json
文件之前完成此操作,以便将其包含在该文件中。通过查看certificate\u 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>"
}
},
“oauth_客户端”:[
{
“客户id”:“,
“客户类型”:1,
“android_信息”:{
“包名称”:“com.google.firebase.quickstart.auth”,
“证书\u哈希”:”
}
},
请将您的帖子仅限于相关部分。您的解决方案奏效了,使用firebase添加证书哈希,而不是在凭据api页面上手动添加。非常感谢。这意味着很多!我在这个问题上纠结了很长时间
"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>"
}
},