使用CredentialManager实现Google登录时触发取消异常求助
Android CredentialManager Google登录选择账号时抛出GetCredentialCancellationException异常
- 问题现象:在Android应用中通过CredentialManager实现Google登录功能,点击登录按钮弹出账号选择弹窗,选择Gmail账号时提示“activity is cancelled by the user”,抛出异常
androidx.credentials.exceptions.GetCredentialCancellationException: activity is cancelled by the user - GCP配置说明:使用web_client_id进行配置,同一应用原本存在两个Android ClientID,已删除AppSigningKey(SHA-1)对应的client_id,仅保留上传密钥库(SHA-1)对应的client_id
相关实现代码如下:
val googleIdOption: GetSignInWithGoogleOption = GetSignInWithGoogleOption.Builder(WEB_CLIENT_ID).build() val request: GetCredentialRequest = GetCredentialRequest.Builder().addCredentialOption(googleIdOption).build() R.id.btn_googleSignUp -> { val credentialManager = CredentialManager.create(this@SignUpActivity) coroutineScope.launch { try { Log.e("request.. ===", request.toString()) val result = credentialManager.getCredential( request = request, context = this@SignUpActivity, ) handleSignIn(result) } catch (e: GetCredentialCancellationException) { Log.e("CredentialCancelException ===", e.message.toString()) } catch (e: GetCredentialException) { Log.e("GetCredential ===", e.message.toString()) } catch (e: Exception) { Log.e("Exception ===", e.message.toString()) } } } fun handleSignIn(result: GetCredentialResponse) { // Handle the successfully returned credential. val credential = result.credential when (credential) { is PublicKeyCredential -> { // Share responseJson such as a GetCredentialResponse on your server to // validate and authenticate //responseJson = credential.authenticationResponseJson } is PasswordCredential -> { // Send ID and password to your server to validate and authenticate. val username = credential.id val password = credential.password } is CustomCredential -> { if (credential.type == GoogleIdTokenCredential.TYPE_GOOGLE_ID_TOKEN_CREDENTIAL) { try { // Use googleIdTokenCredential and extract id to validate and // authenticate on your server. val googleIdTokenCredential = GoogleIdTokenCredential .createFrom(credential.data) } catch (e: GoogleIdTokenParsingException) { Log.e("Invalid GoogleId", "Received an invalid google id token response", e) } } else { // Catch any unrecognized custom credential type here. Log.e("Unexpected", "Unexpected type of credential") } } else -> { // Catch any unrecognized credential type here. Log.e("Invalid GoogleID", "Unexpected type of credential") } } }
内容的提问来源于stack exchange,提问作者sejn
相关产品推荐
相关产品推荐

