一、官网的使用说明开始使用一键登录和注册 | Authentication | Google for Developers二、先到API控制台注册应用添加两个凭证一个web应用凭证和一个android应用凭证注意在开发代码里用的是web应用凭证如果用android凭证使用时会报错“10: Developer console is not set up correctly”不知道为什么如果API控制台不添加android凭证也会报错。另外上面的情况在打包apk时没问题但是如果要打包aab发布到googleplay使用这个功能还是报错这时需要修改android凭证的sha-1从googleplayconsole后台复制如下三、测试需要添加测试用户四、发布还有些要求三、复制客户端ID复制到string.xml中准备工作好开始写activity直接上代码private lateinit var oneTapClient: SignInClient private lateinit var signUpRequest: BeginSignInRequest private var showOneTapUI true private val requestDataLauncher registerForActivityResult(ActivityResultContracts.StartIntentSenderForResult()) { result - try { val credential oneTapClient.getSignInCredentialFromIntent(result.data) val idToken credential.googleIdToken Log.e(TAG, googleIdToken: $idToken) Log.e(TAG, email: ${credential.id}) Log.e(TAG, displayName: ${credential.displayName}) Log.e(TAG, familyName: ${credential.familyName}) Log.e(TAG, givenName: ${credential.givenName}) val regeditBean RegeditBean() regeditBean.googleTokenId idToken regeditBean.username credential.id.substring(0, credential.id.indexOf()) regeditBean.email credential.id regeditBean.firstname credential.givenName regeditBean.lastname credential.familyName when { idToken ! null - { doGoogleLogin(regeditBean) }else - { // Shouldnt happen. Log.e(TAG, No ID token!) } } } catch (e: ApiException) { when (e.statusCode) { CommonStatusCodes.CANCELED - { Log.e(TAG, One-tap dialog was closed.) // Dont re-prompt the user. showOneTapUI false } CommonStatusCodes.NETWORK_ERROR - { Log.e(TAG, One-tap encountered a network error.) // Try again or just ignore. } else - { Log.e(TAG, Couldnt get credential from result. (${e.localizedMessage})) } } } } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) oneTapClient Identity.getSignInClient(this) signUpRequest BeginSignInRequest.builder() .setGoogleIdTokenRequestOptions( BeginSignInRequest.GoogleIdTokenRequestOptions.builder() .setSupported(true) // Your servers client ID, not your Android client ID. .setServerClientId(getString(R.string.google_login_client_web_id)) // .setServerClientId(getString(R.string.google_login_client_debug_id)) // .setServerClientId(getString(R.string.google_login_client_id)) // Show all accounts on the device. .setFilterByAuthorizedAccounts(false) .build()) .build() oneTapClient.beginSignIn(signUpRequest) .addOnSuccessListener(this) { result - try { requestDataLauncher.launch(IntentSenderRequest .Builder(result.pendingIntent.intentSender) .build()) } catch (e: IntentSender.SendIntentException) { Log.e(TAG, Couldnt start One Tap UI: ${e.localizedMessage}) } } .addOnFailureListener(this) { e - // No Google Accounts found. Just continue presenting the signed-out UI. Log.e(TAG, e.localizedMessage) } }需要在build.gradle中添加引用dependencies { ... //一键登录和注册 implementation com.google.android.gms:play-services-auth:20.6.0 ... }