android开发google账号一键登录和注册

一、官网的使用说明

开始使用一键登录和注册  |  Authentication  |  Google for Developers

二、先到API控制台注册应用添加web应用凭证,注意一定是web应用凭证,如果用android凭证使用时会报错“10: Developer console is not set up correctly”不知道为什么

android开发google账号一键登录和注册_第1张图片

三、测试需要添加测试用户

android开发google账号一键登录和注册_第2张图片

四、发布还有些要求

android开发google账号一键登录和注册_第3张图片

三、复制客户端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 -> {
                    // Shouldn't happen.
                    Log.e(TAG, "No ID token!")
                }
            }
        } catch (e: ApiException) {
            when (e.statusCode) {
                CommonStatusCodes.CANCELED -> {
                    Log.e(TAG, "One-tap dialog was closed.")
                    // Don't 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, "Couldn't 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 server's 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, "Couldn't 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'
...
}

你可能感兴趣的:(android)