gpt4 book ai didi

Google 登录的 Android Firebase 身份验证失败

转载 作者:太空宇宙 更新时间:2023-11-03 13:13:51 25 4
gpt4 key购买 nike

我关注了 firebase documentation从上到下并为我的应用程序实现了一个简单的谷歌登录选项。但是,当我尝试登录时,该过程在选择谷歌帐户后停止,并导致 onActivityResult 方法内部出现错误:

if (requestCode == RC_SIGN_IN) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
if (result.isSuccess()) {
// Google Sign In was successful, authenticate with Firebase
GoogleSignInAccount account = result.getSignInAccount();
firebaseAuthWithGoogle(account);
} else {
//THE CODE BREAKS HERE
// Google Sign In failed, update UI appropriately
}
}

代码在 if (result.isSuccess()) 条件处中断。为了进一步详细说明,这是我在阅读文档后实现的完整代码:

创建时:

public class TestLogin extends AppCompatActivity {

private SignInButton mGoogleBtn;
private static int RC_SIGN_IN = 226;
private GoogleApiClient mGoogleApiClient;
private String TAG = "Checkmate";
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;

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

mAuth = FirebaseAuth.getInstance();
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
if(firebaseAuth.getCurrentUser() != null){
//success
}
}
};

mGoogleBtn = (SignInButton) findViewById(R.id.googleBtn);

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken("MY_CLIENT_ID-MY_CLIENT_ID.apps.googleusercontent.com")
.requestEmail()
.build();

mGoogleApiClient = new GoogleApiClient.Builder(getApplicationContext())
.enableAutoManage(this, new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
//Failed
}
})
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();

mGoogleBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
statusMessage.setText("Loading..");
signIn();
}
});
}

登录方法:

当点击 Google Signin 按钮时调用 SignIn 方法:

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

onActivityResult:

onActivityResult 在点击按钮后收到结果时被调用。这是发生错误的地方:

    @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()) {
// Google Sign In was successful, authenticate with Firebase
GoogleSignInAccount account = result.getSignInAccount();
firebaseAuthWithGoogle(account);
} else {
//THE CODE BREAKS HERE
// Google Sign In failed, update UI appropriately
}
}
}

处理 Google 的 Firebase 登录代码:

此方法永远不会被调用,因为代码在 onActivityResult 条件下中断。

    private void firebaseAuthWithGoogle(final GoogleSignInAccount acct) {

AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
mAuth.signInWithCredential(credential)
.addOnCompleteListener(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(TestLogin.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
}
});
}

@Override
protected void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}

list :

Yes, I added the google_services.json file to the /app directory

Yes, I created a new OAuth Client Key in the Google Credentials page

这是我的 gradle:

dependencies {
compile 'com.google.android.gms:play-services:9.6.1'
compile 'com.google.firebase:firebase-core:9.6.1'
compile 'com.google.firebase:firebase-database:9.6.1'
compile 'com.google.firebase:firebase-auth:9.6.1'
compile 'com.google.firebase:firebase-storage:9.6.1'
}
apply plugin: 'com.google.gms.google-services'

还有..

buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.3'
classpath 'com.google.gms:google-services:3.0.0'
}
}

最佳答案

在我之前使用 Firebase FriendlyChat 的测试应用程序中,我在登录时遇到了类似的失败,但我已经通过转到 Google API 控制台(https://console.developers.google.com/apis/credentials?project=)然后下载“client_secret.json”解决了这个问题(在我的例子中,我需要要为您的 Android 客户端重命名下载的文件名“client_secret_xxxxx.json”),请将此文件放入您的 AndroidStudio 项目中的“/src/main/resources”下(如果不存在则创建“resources”文件夹)然后尝试编译并运行您的 Android再次应用。

总结:您需要 2 个 json 文件:来自 Firebase 控制台的“google-services.json”和来自 Google API 控制台的“client_secret.json”。

希望这能解决您的问题。

关于Google 登录的 Android Firebase 身份验证失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39900425/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com