firebase java. lang.非法参数异常:必须指定idToken或accessToken

xe55xuns  于 2023-03-09  发布在  Java
关注(0)|答案(4)|浏览(135)

I am trying to implement google sign in authentication using firebase. I was following this tutorial.
Error log :
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1002, result=-1, data=Intent { (has extras) }} to activity {com.clabs.codefosterapp/com.clabs.codefosterapp.SplashActivity}: java.lang.IllegalArgumentException: Must specify an idToken or an accessToken.
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)
Crashing at following line

AuthCredential credential = GoogleAuthProvider.getCredential(account.getIdToken(), null);

My code :

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();
                        }
                    }
                });
    }
brgchamk

brgchamk1#

我检查了我的整个代码,发现我在构建GoogleSignInOptions时没有设置requestIdToken。正确的代码是:

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

z4iuyo4d2#

您需要从Firebase中的应用 Jmeter 板获取Web客户端ID并将其粘贴到此处。

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
               .requestIdToken("firebase_web_client_id_for_google")
               .requestEmail()
               .build();
wkyowqbh

wkyowqbh3#

// Initialize Firebase Auth

mAuth = FirebaseAuth.getInstance();

这就是为什么需要使用内标识来指定

v1l68za4

v1l68za44#

在我的情况下,我忘记添加谷歌登录到Firebase控制台内的身份验证

然后,您可以访问客户端ID,如下所示

val signInOptions = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
        .requestIdToken(context.getString(R.string.default_web_client_id))
        .requestEmail()
        .build()

如果您了解上下文,或

val signInOptions = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
        .requestIdToken(getString(R.string.default_web_client_id))
        .requestEmail()
        .build()

或者,查看一下firebase,您将在那里获得令牌

val signInOptions = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
        .requestIdToken("paste your id that points to the blue arrow")
        .requestEmail()
        .build()

相关问题