Edz*_*ski 3 java android firebase google-cloud-platform firebase-authentication
我想使用 Google 帐户登录我的应用程序。我使用 Firebase 对用户进行身份验证。
我已经阅读了该文档一百万遍。我已将 SH1 调试密钥作为指纹添加到 Firebase 项目中,并使用Web SDK configurationFirebase 作为 Web 客户端 ID:

我已经在 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
});
});
}
}
Run Code Online (Sandbox Code Playgroud)
这不是我尝试实现它的唯一方法。我还尝试将 sh1 密钥添加到https://console.cloud.google.com/并从https://console.cloud.google.com/apis/credentials?project=myproject站点配置 OAuth 2.0:
但实际上没有任何作用。
我总是得到的是Log.d("googleLoginButton", "oneTapClient.beginSignIn:onError - " + e.getMessage());->Cannot find a matching credential.
我不知道我做错了什么。请注意,我已经setFilterByAuthorizedAccounts设置为false.
我怎样才能让它发挥作用?我做错了什么?
我已经读过:
在 Android 上进行一键登录和注册时收到“16:找不到匹配的凭据”
Firebase Google auth,注销并再次登录将使用上次签名的帐户登录
当用户注销我的 Firebase 应用程序时,为什么它不也从身份验证提供商处注销(例如 Google)?
Google Firebase 在 Android 应用中注销并忘记用户
没有任何帮助。
为了能够在应用程序中通过 Google 进行身份验证时选择帐户,您必须首先在启动应用程序的设备上使用该帐户进行身份验证。
如果您在代码中使用,.setFilterByAuthorizedAccounts(false)则意味着您想要显示设备上的所有帐户。如果您未通过任何帐户进行身份验证,则除了异常之外不会显示任何内容。
如果您希望能够在多个帐户之间进行选择,则必须使用设备上的每个帐户登录。
我使用过其他应用程序,几乎总是可以选择使用新帐户进行身份验证,而无需将其添加到设备中。
所有 Android 设备都至少关联一个 Google 帐户。因此,为了能够在应用程序中使用 Google 登录,您必须首先在设备上进行身份验证或要求使用 Google 帐户登录。
| 归档时间: |
|
| 查看次数: |
831 次 |
| 最近记录: |