问题描述
我正在尝试使用Firebase实施google登录身份验证.我正在关注此教程.
I am trying to implement google sign in authentication using firebase.I was following this tutorial.
错误日志:
(ActivityThread.java:3389) 在 android.app.ActivityThread.handleSendResult(ActivityThread.java:3432) 在android.app.ActivityThread.access $ 1300(ActivityThread.java:135) 在 android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1244) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:136) 在android.app.ActivityThread.main(ActivityThread.java:5045) 在java.lang.reflect.Method.invokeNative(本机方法) 在java.lang.reflect.Method.invoke(Method.java:515) 在 com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 在dalvik.system.NativeStart.main(本机方法) 原因:java.lang.IllegalArgumentException:必须指定一个idToken 或accessToken. 位于com.google.firebase.auth.GoogleAuthCredential.(未知 来源) 在com.google.firebase.auth.GoogleAuthProvider.getCredential(未知 来源) 在 com.clabs.codefosterapp.SplashActivity.firebaseAuthWithGoogle(SplashActivity.java:102) 在 com.clabs.codefosterapp.SplashActivity.onActivityResult(SplashActivity.java:91) 在android.app.Activity.dispatchActivityResult(Activity.java:5423) 在android.app.ActivityThread.deliverResults(ActivityThread.java:3385) 在 android.app.ActivityThread.handleSendResult(ActivityThread.java:3432) 在android.app.ActivityThread.access $ 1300(ActivityThread.java:135) 在 android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1244) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:136) 在android.app.ActivityThread.main(ActivityThread.java:5045) 在java.lang.reflect.Method.invokeNative(本机方法) 在java.lang.reflect.Method.invoke(Method.java:515) 在 com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 在dalvik.system.NativeStart.main(本机方法)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3389) at android.app.ActivityThread.handleSendResult(ActivityThread.java:3432) at android.app.ActivityThread.access$1300(ActivityThread.java:135) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5045) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.IllegalArgumentException: Must specify an idToken or an accessToken. at com.google.firebase.auth.GoogleAuthCredential.(Unknown Source) at com.google.firebase.auth.GoogleAuthProvider.getCredential(Unknown Source) at com.clabs.codefosterapp.SplashActivity.firebaseAuthWithGoogle(SplashActivity.java:102) at com.clabs.codefosterapp.SplashActivity.onActivityResult(SplashActivity.java:91) at android.app.Activity.dispatchActivityResult(Activity.java:5423) at android.app.ActivityThread.deliverResults(ActivityThread.java:3385) at android.app.ActivityThread.handleSendResult(ActivityThread.java:3432) at android.app.ActivityThread.access$1300(ActivityThread.java:135) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5045) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) at dalvik.system.NativeStart.main(Native Method)
在以下行崩溃
AuthCredential credential = GoogleAuthProvider.getCredential(account.getIdToken(), null);
我的代码:
private void googleSignIn() {
Intent intent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(intent, SIGN_IN);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SIGN_IN) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
if (result.isSuccess()) {
GoogleSignInAccount account = result.getSignInAccount();
firebaseAuthWithGoogle(account);
} else {
Toast.makeText(SplashActivity.this, "Oops! Something Went Wrong", Toast.LENGTH_SHORT).show();
}
}
}
private void firebaseAuthWithGoogle(GoogleSignInAccount account) {
AuthCredential credential = GoogleAuthProvider.getCredential(account.getIdToken(), null);
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (!task.isSuccessful()) {
Toast.makeText(SplashActivity.this, "Authentication Failed", Toast.LENGTH_SHORT).show();
}
}
});
}
推荐答案
我正在查看我的整个代码,发现在构建GoogleSignInOptions时未设置requestIdToken.正确的代码是:
I was reviewing my entire code and I found that i didn't set the requestIdToken while building GoogleSignInOptions.The correct code is:
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();
这篇关于java.lang.IllegalArgumentException:必须指定idToken或accessToken的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!