淄博网站备案,wordpress 前台上传图片,欧洲vpswindows直播,常州网站关键词推广背景#xff1a;
Android 需要用到Google的登录授权#xff0c;用去token给到服务器#xff0c;服务器再通过token去获取用户信息#xff0c;实现第三方登录。
我们通过登录之后的email来获取token#xff0c;不需要server_clientId#xff1b;如果用server_clientId还…背景
Android 需要用到Google的登录授权用去token给到服务器服务器再通过token去获取用户信息实现第三方登录。
我们通过登录之后的email来获取token不需要server_clientId如果用server_clientId还需要在google的控制台配置测试的账号否则登录的时候会返回错误码10. 实现步骤
1、 手机或者Pad连接可以访问google的网络
2、最外层的build.gradle增加依赖
dependencies {classpath com.google.gms:google-services:4.3.15
}
app下的build.gradle增加依赖
implementation com.google.android.gms:play-services-auth:20.6.0
3、初始化谷歌服务
val googleSignInOptions GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail() .build()
//这里不要调用requestIdToken
mGoogleSignInClient GoogleSignIn.getClient(activity, googleSignInOptions)
4、检查google 服务器是否可用手机是否有环境
val isGooglePlayServicesAvailableGoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(activity)
5、如果没有环境返回流程结束
if (isGooglePlayServicesAvailable! ConnectionResult.SUCCESS) {// 验证是否已在此设备上安装并启用Google Play服务以及此设备上安装的旧版本是否为此客户端所需的版本GoogleApiAvailability.getInstance().makeGooglePlayServicesAvailable(activity)returnlaunchIO
} 6、如果有环境调用google的登录调转到第三方app
val signInIntent: Intent mGoogleSignInClient!!.signInIntent
activity.startActivityForResult(signInIntent,100
) 7、重写Activity的这个方法然后获取token
fun onActivityResult(requestCode: Int, data: Intent?) {if (requestCode 100) {val completedTask: TaskGoogleSignInAccount GoogleSignIn.getSignedInAccountFromIntent(data)
val account: GoogleSignInAccount completedTask.getResult(ApiException::class.java)
val status: TaskGoogleSignInAccount completedTask
if (!status.isSuccessful) {throw Exception(status.exception)
}
Log.d(测试, account.email: account.email) var loginGoogleRequest LoginGoogleRequest() loginGoogleRequest.bizId CoreConstant.bizId // Obtain token for access gmail account var token: String GoogleAuthUtil.getToken( CoreVariable.coreApplication, account.email!!, oauth2:profile email )Log.d(测试, token token) }