注册

Android集成开发google登录

这是我参与新手入门的第2篇文章


背景



项目在要Google Play上架,并支持全球下载,加了google登录



一.准备


google登录集成地址



  1. 在google登录中创建并配置项目:console.developers.google.com

在控制面板选择Credentials → New Project,会提示创建项目名称和组织名称,如下图


WX20210708-135551.png 2. 创建项目成功后开始创建OAuth client ID image.png 应用类型选择为Android


image.png 根据系统提示,名称, packageName以及SHA-1值 获取SHA-1值的方式: keytool -keystore path-to-debug-or-production-keystore -list -v


image.png


创建成功后会生成一个Client ID 一定要保存好,集成的时候要用



PS: 如果是通过集成文档创建成功的,会提示下载credentials.json文件,一定要下,不然可能会坑



image.png


二.集成开发



PS: google登录需要运行在Android 4.1及以上且Google Play 服务 15.0.0及以上版本




  • 把刚才下载的credentials.json文件放入app路径的根目录
  • 检查项目顶级build.gradle中包含Maven库

allprojects {
repositories {
google()

// If you're using a version of Gradle lower than 4.1, you must instead use:
// maven {
// url 'https://maven.google.com'
// }
}
}

在app的build.gradle中引用google play服务


dependencies {
implementation 'com.google.android.gms:play-services-auth:19.0.0'
}

添加登录



  • 配置 Google Sign-in 和 GoogleSignInClient 对象

var mGoogleSignInClient: GoogleSignInClient? = null
private fun initGoogle() {
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(CommonConstants.GOOGLE_CLIENT_ID)
.requestEmail()
.build()
mGoogleSignInClient = GoogleSignIn.getClient(requireActivity(), gso)
}


CommonConstants.GOOGLE_CLIENT_ID 为创建项目成功后的Client ID




  • 调起登录

private fun signIn() {
val signInIntent: Intent = mGoogleSignInClient?.signInIntent!!
startActivityForResult(signInIntent, RC_SIGN_IN)
}


  • onActivityResult中接收消息

    private val RC_SIGN_IN: Int = 3000
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
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)
}
super.onActivityResult(requestCode, resultCode, data)
}

private fun handleSignInResult(completedTask: Task<GoogleSignInAccount>) {
try {
val account = completedTask.getResult(ApiException::class.java)
// Signed in successfully, show authenticated UI.
Log.e("handleSignInResult", account.toString())
Log.e("handleSignInResult_displayName", account?.displayName!!)
Log.e("handleSignInResult_email", account?.email!!)
Log.e("handleSignInResult_familyName", account?.familyName!!)
Log.e("handleSignInResult_givenName", account?.givenName!!)
Log.e("handleSignInResult_id", account?.id!!)
Log.e("handleSignInResult_idToken", account?.idToken!!)
Log.e("handleSignInResult_isExpired", account?.isExpired.toString())
Log.e("handleSignInResult_photoUrl", account?.photoUrl.toString())
} catch (e: ApiException) {
// The ApiException status code indicates the detailed failure reason.
// Please refer to the GoogleSignInStatusCodes class reference for more information.
Log.e("handleSignInResult", "signInResult:failed code=" + e.statusCode)
}
}

检查现有用户是否登录


 val account = GoogleSignIn.getLastSignedInAccount(activity)
updateUI(account);

退出登录


val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(CommonConstants.GOOGLE_CLIENT_ID)
.requestEmail()
.build()
val mGoogleSignInClient = GoogleSignIn.getClient(activity, gso)
mGoogleSignInClient.signOut().addOnCompleteListener(activity) { }

拿到token信息后发送至自己的服务进行校验,至此google登录完成


作者:是芝麻吖
链接:https://juejin.cn/post/6982454523621015589
来源:掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

0 个评论

要回复文章请先登录注册