You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jetpack Compose中CredentialManager谷歌登录始终返回用户取消异常求助

解决Jetpack Compose中谷歌登录GetCredentialCancellationException异常

以下是针对你遇到问题的排查和解决方案:

1. 确认OAuth同意屏幕配置完整

  • 登录Google Cloud Console,进入API和服务 > OAuth同意屏幕
  • 确保已完成所有必填项填写,若处于测试状态,必须添加测试用户(使用你要登录的Google账号)
  • 未配置测试用户的话,非白名单用户发起登录会直接被拦截,触发取消异常

2. 验证客户端ID及指纹配置

  • 确认使用的WEB_APPLICATION_ID是Web应用类型的客户端ID(而非Android客户端ID)
  • 检查该Web客户端ID对应的项目中,已添加应用的SHA-1和SHA-256指纹(在API和服务 > 凭据页面,找到Web客户端ID查看已配置的指纹)
  • 同时确保Android客户端ID(若已创建)也添加了相同指纹,避免跨客户端权限问题

3. 检查登录流程实现是否完整

你只提供了登录选项代码,需确保完整实现CredentialsManager的调用流程,在Compose中正确处理ActivityResult:

import android.app.Activity
import android.content.Context
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.credentials.Credential
import androidx.credentials.CredentialsManager
import androidx.credentials.GetCredentialRequest
import androidx.credentials.exceptions.GetCredentialCancellationException
import androidx.credentials.playservices.auth.GetSignInWithGoogleOption
import androidx.lifecycle.LifecycleCoroutineScope
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.result.IntentSenderRequest

@Composable
fun GoogleSignInComponent(context: Context) {
    val credentialsManager = remember { CredentialsManager.create(context) }
    val coroutineScope = rememberCoroutineScope()
    val signInLauncher = rememberLauncherForActivityResult(
        contract = ActivityResultContracts.StartIntentSenderForResult()
    ) { result ->
        if (result.resultCode == Activity.RESULT_OK) {
            result.data?.let { data ->
                coroutineScope.launch {
                    runCatching {
                        credentialsManager.getCredentialFromIntent(data)
                    }.onSuccess { credential ->
                        handleGoogleCredential(credential)
                    }.onFailure { e ->
                        if (e is GetCredentialCancellationException) {
                            // 处理用户取消逻辑,若异常非主动触发需排查其他问题
                        }
                    }
                }
            }
        }
    }

    // 绑定到按钮点击的登录触发函数
    fun triggerGoogleSignIn() {
        val signInOption = GetSignInWithGoogleOption.Builder(WEB_APPLICATION_ID)
            .build()
        val request = GetCredentialRequest.Builder()
            .addCredentialOption(signInOption)
            .build()

        coroutineScope.launch {
            runCatching {
                credentialsManager.getCredential(context, request)
            }.onSuccess { response ->
                val intentSenderRequest = IntentSenderRequest.Builder(
                    response.credentialData.intentSender
                ).build()
                signInLauncher.launch(intentSenderRequest)
            }.onFailure { e ->
                // 处理请求初始化失败的情况
            }
        }
    }
}

private fun handleGoogleCredential(credential: Credential) {
    if (credential is com.google.android.libraries.identity.googleid.GoogleIdTokenCredential) {
        val idToken = credential.idToken
        // 将idToken发送到后端验证,完成登录流程
    }
}

若缺少startIntentSenderForResult的调用流程,登录界面无法正常启动,会直接触发取消异常。

4. 升级依赖版本至兼容稳定版

当前依赖版本可能存在兼容性问题,建议升级到最新稳定版:

// Google Sign In 依赖
implementation 'com.google.android.gms:play-services-auth:21.2.0'
implementation "androidx.credentials:credentials:1.3.0"
implementation "androidx.credentials:credentials-play-services-auth:1.3.0"
implementation "com.google.android.libraries.identity.googleid:googleid:1.2.0"

5. 检查设备环境

  • 确保设备上的Google Play服务已更新至最新版本
  • 测试时使用已添加到OAuth测试用户列表的Google账号,避免权限拦截

内容的提问来源于stack exchange,提问作者Mihai Radu Miclaus

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 18:53:12