[英]Google Sign-in authentication not working
我使用了Stackoverflow,并尝试了建议的答案,但是它们都没有起作用。 我正在尝试使用Firebease和Google验证用户,我的代码工作正常。 但是在这种方法中,我总是使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和我的google API凭据有关。 我想知道是否有人可以给我一些明确的步骤,以解决此问题。 这就是我尝试使登录过程正常进行的工作。
keystore -list -vrt -alias androiddebugkey -keystore〜/ .android / debug。
密码为android
然后,在创建android Auth令牌时输入了此密钥。
我在onCreate的登录类中也包含了所有这些代码
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();
所有这些以及侦听器和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();
}
// ...
}
});
}
我已经为此坚持了很长时间,非常感谢您的帮助。 我不清楚我的问题是什么,我只是猜测是这样。 如果需要任何其他信息,请让我知道,我将更新此帖子。
谢谢
这更多是建议的集合,而不是答案。
更新完成侦听器以记录失败的异常:
if (!task.isSuccessful()) {
Log.w(TAG, "signInWithCredential", task.getException());
Toast.makeText(GoogleSignInActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
运行时,查看logcat
输出以查看异常以及与身份验证处理相关的其他日志消息。
我想知道您发布的这一步骤以及与您的项目的SHA1指纹相关的步骤的顺序:
步骤4.前往Google API凭证网站...
我在多个项目(包括Auth QuickStart)中都进行了auth 认证 ,并且不记得需要使用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>"
}
},
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.