Android集成Google登录

  1. 登录Google开发者控制台
    在这里插入图片描述

  2. 新建项目
    在这里插入图片描述

  3. 创建凭据
    在这里插入图片描述

  4. 选择选择“OAuth 客户端 ID”
    在这里插入图片描述

  5. 选择类型

    1. android(直接对接google登录)
      在这里插入图片描述
      sha1获取失败的解决方法:1.cmd切换到签名文件目录。2.执行keytool -list -v -keystore debug.keystore (debug.keystore是签名文件,根据自己的签名文件填写)
      在这里插入图片描述
    2. web(后端验证/后端访问API)
      在这里插入图片描述
  6. 创建完成后提示已创建
    在这里插入图片描述

  7. 拷贝客户端ID,进入Android应用配置

    <!-- 添加 Google 登录配置 -->
    <meta-data
        android:name="com.google.android.gms.games.APP_ID"
        android:value="@string/app_id" />
    
    <!-- 配置 Google 登录授权 Activity -->
    <activity
        android:name="com.google.android.gms.auth.api.signin.internal.SignInHubActivity"
        android:theme="@android:style/Theme.Translucent.NoTitleBar"
        android:exported="false" />
    
    
  8. android应用gradle引入依赖

    implementation 'com.google.android.gms:play-services-auth:19.0.0'
    
  9. api调用
    Google Sign-In for Android API
    Google Sign-In for Android API 详

  10. 流程
    客户端调用API登录 -》 接收数据(用户名等信息和id token) -》 UI展示/将id token发送给后端 -》 后端校验

  11. 示例

    package com.xxx.xxx.manager
    
    import android.annotation.SuppressLint
    import android.app.Activity
    import android.content.Intent
    import com.google.android.gms.auth.api.signin.GoogleSignIn
    import com.google.android.gms.auth.api.signin.GoogleSignInAccount
    import com.google.android.gms.auth.api.signin.GoogleSignInClient
    import com.google.android.gms.auth.api.signin.GoogleSignInOptions
    import com.google.android.gms.common.api.ApiException
    import com.xxx.lib.utils.LogX
    
    object GoogleAccountManager {
    
        const val RC_SIGN_IN = 1001
        @SuppressLint("StaticFieldLeak")
        private var googleSignInClient: GoogleSignInClient? = null
    
        /**
         * 创建账户
         */
        fun create(activity: Activity) {
            val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestEmail()
                .build()
            googleSignInClient = GoogleSignIn.getClient(activity, gso)
        }
    
        /**
         * 创建账户
         */
        fun create(token: String, activity: Activity) {
            val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestIdToken(token)
                .requestEmail()
                .build()
            googleSignInClient = GoogleSignIn.getClient(activity, gso)
        }
    
        /**
         * 检查是否已经登录
         * @return true 已登录, false 未登录
         */
        fun check(activity: Activity): Boolean {
            val account = GoogleSignIn.getLastSignedInAccount(activity)
            return account != null
        }
    
        /**
         * 登录
         */
        fun login(activity: Activity) {
            googleSignInClient?.let {
                activity.startActivityForResult(it.signInIntent, RC_SIGN_IN)
            }
        }
    
        /**
         * 从返回的intent中获取用户信息
         */
        fun getIntentAccountInfo(intent: Intent?): GoogleSignInAccount? {
            return try {
                val tasks = GoogleSignIn.getSignedInAccountFromIntent(intent)
                tasks.getResult(ApiException::class.java)
            } catch (e: ApiException) {
                LogX.i("signInResult:failed code=${e.statusCode}")
                null
            }
        }
    
        /**
         * 获取已登录用户的个人资料信息
         */
        fun getAccountInfo(activity: Activity) = GoogleSignIn.getLastSignedInAccount(activity)
    
    }
    
    // 登录
    GoogleAccountManager.create(this) // app直接对接使用这个方法
    GoogleAccountManager.create("app_id", this) // 后端校验或后端需访问API使用这个方法
    GoogleAccountManager.login(this) // 登录
    
    // google登录成功后回到Activity
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode === GoogleAccountManager.RC_SIGN_IN) {
            val info = GoogleAccountManager.getIntentAccountInfo(data)
            LogX.i("google sign in, name: ${info?.displayName}, idToken: ${info?.idToken}")
        }
    }
    
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值