带有空令牌的 Firebase phone 身份验证响应

Firebase phone auth response with null token

我正在按照这些官方链接将 Digits 身份验证迁移到 Firebase 身份验证:

google-services.json 从 Firebase 控制台导出的文件已集成到项目中。

我正在使用带 UI 的嵌入式解决方案,这是我的代码:

startActivityForResult(
                AuthUI.getInstance()
                        .createSignInIntentBuilder()
                        .setProviders(Arrays.asList(
                                new AuthUI.IdpConfig.Builder(AuthUI.PHONE_VERIFICATION_PROVIDER).build())).build(), RC_SIGN_IN);

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RC_SIGN_IN) {
        IdpResponse response = IdpResponse.fromResultIntent(data);

        // Successfully signed in
        if (resultCode == RESULT_OK) {
            IdpResponse idpResponse = IdpResponse.fromResultIntent(data);

            //Here retreive the token from the response
            idpResponse.getIdpToken();
            finish();
            return;
        } else {
            // Sign in failed
            if (response == null) {
                // User pressed back button
                return;
            }

            if (response.getErrorCode() == ErrorCodes.NO_NETWORK) {
                Toast.makeText(this, getString(R.string.connection_error), Toast.LENGTH_SHORT).show();
                return;
            }

            if (response.getErrorCode() == ErrorCodes.UNKNOWN_ERROR) {
                Toast.makeText(this, getString(R.string.error_default), Toast.LENGTH_SHORT).show();
                return;
            }
        }

        Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show();
    }
}

这是 build.gradle 中有趣的部分:

android {
    compileSdkVersion 26
    buildToolsVersion '26.0.1'

}

compile 'com.firebaseui:firebase-ui-auth:2.3.0'
compile 'com.android.support:appcompat-v7:26.0.1'
compile 'com.android.support:support-v4:26.0.1'
compile 'com.android.support:recyclerview-v7:26.0.1'
compile 'com.android.support:design:26.0.1'
compile 'com.android.support:support-core-utils:26.0.1'
compile 'com.android.support:support-annotations:26.0.1'
compile 'com.android.support:percent:26.0.1'

当触发 onActivityResult 时,响应成功但令牌始终为 null。

我不知道使用这个身份验证提供程序时,这个令牌是否始终为空,或者我做错了什么。

您需要明确请求令牌。

https://firebase.google.com/docs/auth/admin/verify-id-tokens:

FirebaseUser mUser = FirebaseAuth.getInstance().getCurrentUser();
mUser.getToken(true)

如果登录成功,您应该有一个可用的 Firebase Auth 用户。在这种情况下,没有 IDP 令牌,您应该能够只检索用户:

FirebaseUser currentUser = FirebaseAuth.getInstance().getCurrentUser();