Android Google原生登录

这篇博客介绍了如何在Android应用中集成Google登录功能,包括导入Google服务依赖,并提供了具体的步骤指南,参考了官方文档,同时提供了一个用于处理Google登录的工具类。

导入google依赖

implementation 'com.google.android.gms:play-services-auth:20.1.0'

具体信息可以查看google官方文档
google登录的工具类

class GoogleLoginUtil {

    private val TAG = "GoogleLoginUtil"
    private val RC_SIGN_IN = 9009
    private val googleClientId =
        "google平台获取"

    private var mActivity: Activity;
    private var mGoogleSignInClient: GoogleSignInClient? = null;
    private var mLoginCallBack: LoginCallBack? = null;

    constructor(mActivity: Activity) {
        this.mActivity = mActivity
    }

    interface LoginCallBack {
        fun onLoginSuccess()
        fun onLoginError(code: Int)
    }

    fun init() {
        val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .requestIdToken(googleClientId)
            .requestId()
            .requestProfile()
            .build()
        mGoogleSignInClient = GoogleSignIn.getClient(mActivity, gso);
    }


    fun googleLogin(callBack: LoginCallBack) {
        mLoginCallBack = callBack;
        val account = GoogleSignIn.getLastSignedInAccount(mActivity)
        if (account != null) {
            Log.e(TAG, "account==================================\n\n\n")
            Log.e(TAG, "account ${account.account}")
            Log.e(TAG, "displayName ${account.displayName}")
            Log.e(TAG, "email ${account.email}")
            Log.e(TAG, "familyName ${account.familyName}")
            Log.e(TAG, "givenName ${account.givenName}")
            Log.e(TAG, "grantedScopes ${account.grantedScopes}")
            Log.e(TAG, "id ${account.id}")
            Log.e(TAG, "idToken ${account.idToken}")
            Log.e(TAG, "isExpired ${account.isExpired}")
            Log.e(TAG, "photoUrl ${account.photoUrl}")
            Log.e(TAG, "requestedScopes ${account.requestedScopes}")
            Log.e(TAG, "serverAuthCode ${account.serverAuthCode}")
            Log.e(TAG, "account==================================\n\n\n")
        } else {
            mActivity.runOnUiThread {
                val signInIntent = mGoogleSignInClient?.signInIntent
                mActivity.startActivityForResult(signInIntent, RC_SIGN_IN)
            }
        }


    }


    fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
// Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...);
        // Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...);
        if (requestCode === RC_SIGN_IN) {
            // The Task returned from this call is always completed, no need to attach
            // a listener.
            val task: Task<GoogleSignInAccount> = GoogleSignIn.getSignedInAccountFromIntent(data)
            handleSignInResult(task)
        }
    }

    private fun handleSignInResult(completedTask: Task<GoogleSignInAccount>) {
        try {
            val account = completedTask.getResult(ApiException::class.java)
            if (account != null) {
                Log.e(TAG, "account2==================================\n\n\n")
                Log.e(TAG, "account ${account.account}")
                Log.e(TAG, "displayName ${account.displayName}")
                Log.e(TAG, "email ${account.email}")
                Log.e(TAG, "familyName ${account.familyName}")
                Log.e(TAG, "givenName ${account.givenName}")
                Log.e(TAG, "grantedScopes ${account.grantedScopes}")
                Log.e(TAG, "id ${account.id}")
                Log.e(TAG, "idToken ${account.idToken}")
                Log.e(TAG, "isExpired ${account.isExpired}")
                Log.e(TAG, "photoUrl ${account.photoUrl}")
                Log.e(TAG, "requestedScopes ${account.requestedScopes}")
                Log.e(TAG, "serverAuthCode ${account.serverAuthCode}")
                Log.e(TAG, "account2==================================\n\n\n")
            } else {
                mLoginCallBack?.onLoginError(-2)
            }
            // Signed in successfully, show authenticated UI.
            //updateUI(account)
        } catch (e: ApiException) {
            // The ApiException status code indicates the detailed failure reason.
            // Please refer to the GoogleSignInStatusCodes class reference for more information.
            Log.e(TAG, "signInResult:failed code=" + e.statusCode + " msg " + e.message)
            //updateUI(null)
            mLoginCallBack?.onLoginError(e.statusCode)
        }
    }

    /**
     * 退出登录
     */
    fun signOut() {
        mGoogleSignInClient?.signOut()
            ?.addOnCompleteListener(mActivity) {
                // ...
                //it.result
            }
    }

    /**
     * 断开链接
     */
    fun revokeAccess() {
        mGoogleSignInClient?.revokeAccess()
            ?.addOnCompleteListener(mActivity) {
                // ...
            }
    }


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值