【问题标题】:java.lang.IllegalArgumentException: Must specify an idToken or an accessTokenjava.lang.IllegalArgumentException:必须指定 idToken 或 accessToken
【发布时间】:2017-05-13 18:40:37
【问题描述】:

我正在尝试使用 firebase 实现 google 登录身份验证。 我正在关注this 教程。

错误日志:

java.lang.RuntimeException: 传递结果失败 ResultInfo{who=null, request=1002, result=-1, data=Intent { (有 额外)}}活动 {com.clabs.codefosterapp/com.clabs.codefosterapp.SplashActivity}: java.lang.IllegalArgumentException:必须指定 idToken 或 访问令牌。

在 android.app.ActivityThread.deliverResults(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(Native Method) 在 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 或访问令牌。 在 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(Native Method) 在 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(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();
                        }
                    }
                });
    }

【问题讨论】:

标签: android firebase firebase-authentication google-signin


【解决方案1】:

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

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

【讨论】:

  • 这里是描述,您可以在其中找到 web_client_id stackoverflow.com/questions/34283355/…
  • 是的,我们需要将web客户端id传递给requestIdToken(),web客户端id可以在Firebase控制台->认证->登录方式->谷歌->中找到Web 客户端 ID 配置
  • 虽然我们构建了 Android 应用程序,但我们仍然需要将其传入以使其工作。
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:
    // Initialize Firebase Auth
    
    
    mAuth = FirebaseAuth.getInstance();
    
    

    这就是为什么需要token来指定的原因

    【讨论】:

      猜你喜欢
      • 2017-04-02
      • 2012-01-24
      • 2020-07-13
      • 1970-01-01
      • 2016-05-07
      • 1970-01-01
      • 2021-03-16
      • 2021-05-29
      • 2019-12-23
      相关资源
      最近更新 更多