gpt4 book ai didi

java - 为什么我的 onActivityResult() 总是返回 false?

转载 作者:行者123 更新时间:2023-11-29 23:16:17 24 4
gpt4 key购买 nike

我正在尝试为我的应用程序设置谷歌登录,但它总是失败,我点击一个按钮,弹出一个窗口,选择帐户,弹出一个 Toast 告诉我登录失败。我有什么办法可以解决这个问题?问题出在onActivityResult()方法和所说的 Toast 说“Auth went wrong”。我在整个互联网上进行了搜索,但没有找到解决方案,甚至没有找到发生这种情况的原因。第80行是这个 Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);

public class SignInActivity extends AppCompatActivity {

static final int GOOGLE_SIGN_IN = 2;
FirebaseAuth mAuth;
Button btn_login;
GoogleSignInClient mGoogleSignInClient;
FirebaseAuth.AuthStateListener mAuthListener;

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

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

btn_login = findViewById(R.id.login);
mAuth = FirebaseAuth.getInstance();

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

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();

mGoogleSignInClient = GoogleSignIn.getClient(this, gso);

btn_login.setOnClickListener(v -> signIn());
}

private void signIn() {
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, GOOGLE_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 == GOOGLE_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, "Auth went wrong :/", Toast.LENGTH_SHORT).show();
// ...
}
}
}

private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
Log.d("TAG", "firebaseAuthWithGoogle:" + acct.getId());
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, task -> {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d("TAG", "signInWithCredential:success");
FirebaseUser user = mAuth.getCurrentUser();
Toast.makeText(SignInActivity.this, "Success!", Toast.LENGTH_SHORT).show();
updateUI(user);
Switch();
} 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_SHORT).show();
updateUI(null);
}
});
}
}
2019-03-23 12:13:35.569 6312-6312/com.charpik.agropomocnik W/TAG: Google sign in failed
com.google.android.gms.common.api.ApiException: 10:
at com.google.android.gms.common.internal.ApiExceptionUtil.fromStatus(Unknown Source:4)
at com.google.android.gms.auth.api.signin.GoogleSignIn.getSignedInAccountFromIntent(Unknown Source:8)
at com.charpik.agropomocnik.SignInActivity.onActivityResult(SignInActivity.java:80)
at android.app.Activity.dispatchActivityResult(Activity.java:7701)
at android.app.ActivityThread.deliverResults(ActivityThread.java:5037)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:5084)
at android.app.ActivityThread.-wrap20(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2053)
at android.os.Handler.dispatchMessage(Handler.java:108)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:7529)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)

最佳答案

我使用最新版本的 appcompat 库修复了类似的问题。

implementation 'androidx.appcompat:appcompat:1.1.0-alpha03'

如果不是这种情况,请尝试不使用 requestIdToken(getString(R.string.default_web_client_id))。如果没有发生错误,您应该再次检查凭据部分。

检查这些:

  • SHA-1 指纹是否正确?
  • SHA-1 指纹是用于调试还是发布
  • 包名是否正确?
  • 您是否使用了网络应用程序类型的客户端 ID?

关于java - 为什么我的 onActivityResult() 总是返回 false?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55309698/

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