Firebase OAuth错误:“idp响应中的id_token无效”

时间:2017-01-11 19:01:57

标签: android firebase oauth firebase-authentication google-signin

我似乎已成功实施Google OAuth,以便将其与Firebase一起使用。问题是有时,当我签署&尝试重新登录,它不起作用,我收到错误:

[Invalid id_token in idp response: ...]". 

从这一点开始,SignIn在几天后才开始工作。也许在一定的到期后(我没有找到为什么它再次起作用)。知道如何解决这个问题吗?

这是我的代码(SignInActivity):

private void initializeGoogle() {
    final SignInButton objConnectButton = (SignInButton) findViewById(R.id.signin_google);

    GoogleSignInOptions objOptions = new GoogleSignInOptions
        .Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
        .requestIdToken(C.GOOGLE_CLIENT_ID).build();

    final GoogleApiClient objGoogleApiClient = new GoogleApiClient.Builder(this)
        .enableAutoManage(this, new GoogleApiClient.OnConnectionFailedListener() {
            @Override
            public void onConnectionFailed(@NonNull ConnectionResult aobjResult) {
                C.handleEvent(SignInActivity.this, R.string.msg_signin_connection_failed, aobjResult.getErrorMessage(), 7);
            }
        }).addApi(Auth.GOOGLE_SIGN_IN_API, objOptions).build();

    objConnectButton.setOnClickListener(new View.OnClickListener() {
        @Override public void onClick(View view) {
        startActivityForResult(Auth.GoogleSignInApi.getSignInIntent(objGoogleApiClient),
            C.ReqCode.SIGNIN_GOOGLE);
        }
    });
}

private void signInWithGoogle(final GoogleSignInAccount aobjAccount, AuthCredential aobjCredential) {
    FirebaseAuth.getInstance().signInWithCredential(aobjCredential)
        .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> aobjTask) {
            if (aobjTask.isSuccessful()) {
                startMainActivityWithValues(aobjAccount.getDisplayName(), C.SignInType.GOOGLE);
            }
        }
    })
    .addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception aobjException) {
            C.handleEvent(SignInActivity.this, R.string.msg_signin_connection_failed, aobjException.getMessage(), 12);
        }
    });
}

@Override
protected void onActivityResult(int aintRequestCode, int aintResultCode, Intent aobjDataIntent) {
    super.onActivityResult(aintRequestCode, aintResultCode, aobjDataIntent);

    // Handle Google Auth
    if (aintRequestCode == C.ReqCode.SIGNIN_GOOGLE) {
        if ((aintResultCode == RESULT_OK)) {

            GoogleSignInResult objSignInResult = Auth.GoogleSignInApi.getSignInResultFromIntent(aobjDataIntent);
            if (objSignInResult.isSuccess()) {

                final GoogleSignInAccount objAccount = objSignInResult.getSignInAccount();
                String strTokenID = (objAccount != null ? objAccount.getIdToken() : null);

                if (strTokenID != null) {
                        signInWithGoogle(objAccount, GoogleAuthProvider.getCredential(strTokenID, null));
                } else {
                        C.handleEvent(SignInActivity.this, R.string.msg_signin_connection_failed, getString(R.string.msg_signin_connection_invalid_token), 10);
                }
            } else {
                C.handleEvent(SignInActivity.this, R.string.msg_signin_connection_failed, objSignInResult.getStatus().getStatusMessage(), 8);
            }
        } else {
                C.handleEvent(SignInActivity.this, R.string.msg_signin_connection_cancelled, "", 9);
        }
    }
}

private void startMainActivityWithValues(String astrUsername, int aintAuthType) {
    Intent objIntent = new Intent(SignInActivity.this, MainActivity.class);

    objIntent.putExtra("USER_NAME", astrUsername);
    objIntent.putExtra("AUTH_SOURCE", aintAuthType);

    startActivity(objIntent);
    finish();
}

private void signOutFromGoogle() {
    final GoogleApiClient objClient = new GoogleApiClient.Builder(MainActivity.this)
        .addApi(Auth.GOOGLE_SIGN_IN_API, new GoogleSignInOptions.Builder(
        GoogleSignInOptions.DEFAULT_SIGN_IN).build()).build();

    objClient.registerConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
        @Override
        public void onConnected(@Nullable Bundle aobjBundle) {
            if (objClient.isConnected()) {
                objFbAuth.signOut();
                Auth.GoogleSignInApi.signOut(objClient).setResultCallback(new ResultCallback<Status>() {
                    @Override
                    public void onResult(@NonNull Status aobjStatus) {
                        if (aobjStatus.isSuccess()) {
                            startActivity(new Intent(MainActivity.this, SignInActivity.class));
                            finish();
                            Toast.makeText(MainActivity.this, getString(R.string.msg_signin_logged_out), Toast.LENGTH_SHORT).show();
                        } else {
                            C.handleEvent(MainActivity.this, R.string.msg_signin_disconnection_failed, "", 14);
                        }
                    }
                });
            }
        }

        @Override
        public void onConnectionSuspended(int aintCause) {
            String strMessage = "ConnectionSuspended (" + (aintCause == 1 ? "SERVICE_DISCONNECTED" : "NETWORK_LOST") + ")";
            C.handleEvent(getBaseContext(), R.string.msg_signin_connection_failed, strMessage, 2);
        }
    });

    objClient.connect();
}

2 个答案:

答案 0 :(得分:0)

这是一个似乎有效的解决方案:在Android Studio中,打开文件菜单,然后点击&#34; Invalidate&amp;重新启动&#34 ;.另外:重启Bluestacks(通过托盘图标关闭它)。也许只需要其中一个步骤,但我还没有尝试过。

答案 1 :(得分:0)

我在晚上面临同样的问题,但第二天早上我检查过,它正在工作。只需确保您的firebase配置中存在App Signing屏幕(Google play conosle)中的SHA1证书指纹。