导入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==================================nnn")
            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==================================nnn")
        } 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==================================nnn")
                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==================================nnn")
            } 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) {
                // ...
            }
    }


}

原文地址:https://blog.csdn.net/wittybread/article/details/123738586

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_37172.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注