我想使用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:
我已经将其设置在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:
但实际上什么都不管用.
我总是得到的只有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个
Firebase Google auth, signing out and logging in again will log in with the last signed account个
Google Firebase sign out and forget user in Android app个
什么都没有用.