我想使用Google帐户登录我的应用程序.我使用Firebase对用户进行身份验证.

I've already read the documentation like million times. I've added SH1 debug key to Firebase project as a fingerprint and I use Web SDK configuration from the Firebase as a web client id: this thing

我已经将其设置在strings.xml文件中

<string name="web_client_id">**HERE**</string>

我的其余代码如下所示:

public class AuthFragment extends Fragment {
    private FragmentAuthBinding binding;
    private SignInClient oneTapClient;
    ActivityResultLauncher<Intent> oneTapLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
        result -> {
            if (result.getResultCode() == Activity.RESULT_OK) {
                Intent data = result.getData();
                try {
                    var credential = oneTapClient.getSignInCredentialFromIntent(data);
                    String idToken = credential.getGoogleIdToken();
                } catch (ApiException e) {
                    throw new RuntimeException(e);
                }
            }
        });
    public AuthFragment() {}

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        binding = FragmentAuthBinding.inflate(inflater, container, false);
        return binding.getRoot();
    }

    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        
        HandleGoogleLogin();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }

    void HandleGoogleLogin()
    {
        oneTapClient = Identity.getSignInClient(requireActivity());
        var signInRequest = BeginSignInRequest.builder()
                .setGoogleIdTokenRequestOptions(BeginSignInRequest.GoogleIdTokenRequestOptions.builder()
                        .setSupported(true)
                        .setServerClientId(getString(R.string.web_client_id))
                        .setFilterByAuthorizedAccounts(false)
                        .build())
                .build();

        var googleLoginButton = binding.googleLoginButton;
        googleLoginButton.setOnClickListener(v -> {
            oneTapClient.beginSignIn(signInRequest)
                    .addOnSuccessListener(requireActivity(), result -> {
                        var intentSenderRequest = new IntentSenderRequest
                                .Builder(result.getPendingIntent().getIntentSender())
                                .build();
                        oneTapLauncher.launch(intentSenderRequest.getFillInIntent());
                    })
                    .addOnFailureListener(requireActivity(), e -> {
                        Log.d("googleLoginButton", "oneTapClient.beginSignIn:onError - " + e.getMessage());
                        // Handle errors or continue presenting the signed-out UI
                    });
        });
    }
}

这并不是我试图实现它的唯一方式.我还try 将SH1键添加到https://console.cloud.google.com/,并从https://console.cloud.google.com/apis/credentials?project=myproject站点配置OAuth 2.0:

cloud.google

但实际上什么都不管用.

我总是得到的只有Log.d("googleLoginButton", "oneTapClient.beginSignIn:onError - " + e.getMessage());--Cannot find a matching credential.

我不知道我做错了什么.请注意,我已将setFilterByAuthorizedAccounts设置为false.

我怎么才能让它起作用呢?我做错了什么?

我已经读过了:

Getting "16: Cannot find a matching credential" when doing One Tap sign-in and sign-up on Android

Google One Tap Sign In

Firebase Google auth, signing out and logging in again will log in with the last signed account

When users SignOut of my Firebase app, why doesn't it also SignOut from the auth provider, say Google?

Google Firebase sign out and forget user in Android app

https://medium.com/firebase-developers/how-to-authenticate-to-firebase-using-google-one-tap-in-jetpack-compose-60b30e621d0d

什么都没有用.

推荐答案

为了能够在你的应用程序中向谷歌进行身份验证时 Select 一个帐户,你必须首先在启动该应用程序的设备上通过该帐户的身份验证.

如果你在你的代码中使用.setFilterByAuthorizedAccounts(false),这意味着你想要显示设备上的所有账户.如果您未使用任何帐户进行身份验证,则除了例外,没有其他可显示的内容.

如果您希望能够在多个帐户之间进行 Select ,则必须使用设备上的每个帐户登录.

我使用过其他应用程序,几乎总是可以 Select 使用新帐户进行身份验证,而无需将其添加到设备中.

所有搭载安卓系统的设备都至少关联了一个谷歌账户.因此,为了能够在你的应用程序中登录谷歌,你必须首先在你的设备上进行身份验证,或者被要求使用谷歌账户登录.

Java相关问答推荐

Java应用程序崩溃时试图读取联系人从电话

参数值[...]与预期类型java.util.Date不匹配

为什么我要创建一个单独的互斥体/锁对象?

如何在带有Micronaut的YAML中使用包含特殊字符的字符串作为键

SpringBootreact 式Web应用程序的Spring Cloud Configer服务器中的资源控制器损坏

Java中如何根据Font.canDisplay方法对字符串进行分段

如何让JVM在SIGSEGV崩溃后快速退出?

使用PDFBox从PDF中删除图像

继续收到错误SQLJDBC EXCEPTION执行";org.springframework.dao.InvalidDataAccessResourceUsageException:&

Groovy/Java:匹配带引号的命令选项

在一行中检索字符分隔字符串的第n个值

IntelliJ IDEA中的JavaFX应用程序无法在资源中找到CSS文件

如何从命令行编译包中的所有类?

为什么创建Java动态代理需要接口参数

Java CDI:@Singleton@Startup@Inject无法实现接口

如何通过用户ID向用户发送私信

当我将JTextField的getText函数与相等的String进行比较时;t返回true

Java返回生成器的实现

ExecutorService:如果我向Executor提交了太多任务,会发生什么?

如何在 Android Studio 中删除 ImageView 和屏幕/父级边缘之间的额外空间?