![](/img/trans.png)
[英]Why is Sign in With Google example code returning the wrong token to login to Firebase?
[英]Why Sign In With Google using Firebase not work with me?
我正在尝试将Google Sihn In 与 Firebase 一起使用,但它不起作用。 我已经尝试了几个小时,请帮忙。
这是我的代码:
public class LoginActivity extends AppCompatActivity {
private RelativeLayout signInWithGoogleBtn;
private GoogleSignInOptions gso;
private GoogleSignInClient mGoogleSignInClient;
private static final int RC_SIGN_IN = 123;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
signInWithGoogleBtn = findViewById(R.id.signInWithGoogleBtn);
mAuth = FirebaseAuth.getInstance();
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
signInWithGoogleBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
googleSignIn();
}
});
}
private void googleSignIn() {
Intent intent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(intent, RC_SIGN_IN);
}
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_SIGN_IN) {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
try {
GoogleSignInAccount account = task.getResult(ApiException.class);
firebaseAuthWithGoogle(account.getIdToken());
} catch (ApiException e) {
Toast.makeText(LoginActivity.this, "Error", Toast.LENGTH_SHORT).show();
}
}
}
private void firebaseAuthWithGoogle(String idToken) {
AuthCredential credential = GoogleAuthProvider.getCredential(idToken, null);
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
Toast.makeText(LoginActivity.this, "Google Sign In Success", Toast.LENGTH_SHORT).show();
FirebaseUser user = mAuth.getCurrentUser();
startActivity(new Intent(LoginActivity.this, MainActivity.class));
LoginActivity.this.finish();
} else {
Toast.makeText(LoginActivity.this, "Failed to Sign In with Google! Please check your credentials.", Toast.LENGTH_LONG).show();
}
}
});
}
}
发生的情况是,当我按下“使用 Google 登录”按钮并选择帐户时,会出现 Toast 中的“错误消息”。
请帮助,我将非常感激
当Task
失败时,它会包含一个异常,其中包含有关失败原因的详细信息。 记录该错误消息,您可以查看失败的原因并修复它。
if (task.isSuccessful()) {
...
} else {
Log.e("SIGNIN", "Failed to Sign In with Google", task.getException()); // 👈
Toast.makeText(LoginActivity.this, "Failed to Sign In with Google! Please check your credentials.", Toast.LENGTH_LONG).show();
}
好吧,我解决了我的问题。我没有将应用程序的 SHA-1 放入 Firebase 项目中。 我安装后,问题就消失了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.