-
新建项目
-
创建凭据
-
选择选择“OAuth 客户端 ID”
-
选择类型
- android(直接对接google登录)
sha1获取失败的解决方法:1.cmd切换到签名文件目录。2.执行keytool -list -v -keystore debug.keystore (debug.keystore是签名文件,根据自己的签名文件填写)
- web(后端验证/后端访问API)
- android(直接对接google登录)
-
创建完成后提示已创建
-
拷贝客户端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" />
-
android应用gradle引入依赖
implementation 'com.google.android.gms:play-services-auth:19.0.0'
-
api调用
Google Sign-In for Android API
Google Sign-In for Android API 详 -
流程
客户端调用API登录 -》 接收数据(用户名等信息和id token) -》 UI展示/将id token发送给后端 -》 后端校验 -
示例
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}") } }
Android集成Google登录
最新推荐文章于 2024-04-12 19:21:49 发布