Android中Firebase OIDC的startActivityForSignInWithProvider报错求助
Firebase OIDC登录报错:获取包证书哈希失败
我正在Android中通过OIDC提供商实现登录,已遵循Firebase官方文档操作。点击执行startActivityForSignInWithProvider方法的登录按钮时,状态栏短暂闪烁,LogCat抛出异常:There was an error while trying to get your package certificate hash.,相关堆栈见对应截图。
已做大量排查,所有相关内容都指向添加SHA-1/SHA-256,且我已通过以下命令获取哈希值并添加到Firebase控制台:
keytool -list -v -alias androiddebugkey -keystore ~/.android/debug.keystore
运行Firebase官方Auth示例应用也出现相同错误。应用中Google登录、Deep Link等其他认证方式均正常,仅startActivityForSignInWithProvider方法有问题。文档中的signInWithCredential方法可正常工作,但需要额外库获取IDToken。期望startActivityForSignInWithProvider能正常返回用户对象,已卡多日,请求帮助。
我的代码如下:
class LoginWithFirebaseOidcActivity : AppCompatActivity() { private lateinit var auth: FirebaseAuth private lateinit var binding: ActivityLoginWithFirebaseOidcBinding override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = ActivityLoginWithFirebaseOidcBinding.inflate(layoutInflater) val view = binding.root setContentView(view) auth = Firebase.auth binding.btnLoginWithLiberland.setOnClickListener { // Login using Firebase OIDC library loginUsingFirebaseOidc() } } override fun onStart() { super.onStart() // Check if user is signed in (non-null) and update UI accordingly. val currentUser = auth.currentUser updateUI(currentUser) // Look for a pending auth result val pending = auth.pendingAuthResult if (pending != null) { pending.addOnSuccessListener { authResult -> Log.d(TAG, "checkPending:onSuccess:$authResult") updateUI(authResult.user) }.addOnFailureListener { e -> Log.w(TAG, "checkPending:onFailure", e) } } else { Log.d(TAG, "checkPending: null") } } private fun loginUsingFirebaseOidc() { Log.i(TAG, "loginUsingFirebaseOidc() invoked") // Create authentication providers val providerBuilder = OAuthProvider.newBuilder("oidc.liberland-services-dev") providerBuilder.scopes = listOf("openid", "email", "profile", "phone_number") auth.startActivityForSignInWithProvider(this, providerBuilder.build()) .addOnSuccessListener { Log.i(TAG, "AuthResult: $it") // User is signed in. // IdP data available in // authResult.getAdditionalUserInfo().getProfile(). // The OAuth access token can also be retrieved: // ((OAuthCredential)authResult.getCredential()).getAccessToken(). // The OAuth secret can be retrieved by calling: // ((OAuthCredential)authResult.getCredential()).getSecret(). updateUI(it.user) }.addOnFailureListener { e -> // Handle failure. Log.e(TAG, "Exception: ${e.message}") e.printStackTrace() binding.txtUserStatus.text = "Error logging in: ${e.message}" showToast("Error logging in: ${e.message}") } } private fun updateUI(user: FirebaseUser?) { if (user != null) { binding.txtUserStatus.text = "Logged in as ${user.email}" } else { binding.txtUserStatus.text = "Login result will appear here" } } private fun showToast(message: String) { Toast.makeText(this, message, Toast.LENGTH_SHORT).show() } companion object { private const val TAG = "LoginWithFirebaseOidcActivity" } }
内容的提问来源于stack exchange,提问作者Sachin Agrawal
相关产品推荐
相关产品推荐

