1. 在Google Cloud平台,创建项目
官方网址: https://console.cloud.google.com/
2. 开启Goole登录功能权限
将平台项目下生成的Google servise的json文件拷入到项目根目录中。
3. 创建Android客户端ID
在凭据栏,去创建OAuth2.0客户端。填入软件包名,使用正式签名的keystore,获取其sha1值。
sha1值获取命令:
keytool -keystore path-to-debug-or-production-keystore -list -v
4. 再创建一个Web应用
-
凭据创建完成
注意:要使用使用Web客户端的ID使用到google登录SDK中
6. Google登录 SDK 接入
6.1 添加依赖:
implementation 'com.google.android.gms:play-services-auth:20.5.0'
6.2 创建google登录管理类
/**
* Google登录
* */
class GoogleLogin(private val ctx: Activity) {
private var callback: ILoginCallback? = null
private var mGoogleSignInClient: GoogleSignInClient? = null
/**
* 创建google客户端sdk登录对象
* */
private fun signInClient() {
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestScopes(Scope("https://www.googleapis.com/auth/classroom.courses"))
.requestIdToken("637576167720-013s6603l5i4e6ukg3mrvn4669hdggbg.apps.googleusercontent.com")
.requestProfile()
.requestServerAuthCode("637576167720-013s6603l5i4e6ukg3mrvn4669hdggbg.apps.googleusercontent.com", true)
.requestEmail()
.build();
// Build a GoogleSignInClient with the options specified by gso.
mGoogleSignInClient = GoogleSignIn.getClient(ctx, gso)
}
/**
* Google登录接口
*
* @param callback 登录回调
* */
fun login(callback: ILoginCallback?) {
this.callback = callback
if (mGoogleSignInClient == null) {
signInClient()
}
ctx.startActivityForResult(mGoogleSignInClient?.signInIntent, 1000)
}
}
requestScopes()方法传入登录凭证可以访问的数据范围,这里选择了google课堂的数据。
requestIdToken()方法表示需要获取到idToken。requestProfile()表示需要访问到个人资料。
requestEmail()需要个人邮箱数据。requestServerAuthCode()方法传入clientId,表示需要身份认证code。
6.3 调起google登录并获取登录凭证token
GoogleLogin(this).login(object: ILoginCallback {
override fun onLoginFailed(code: Int, msg: String) {
}
override fun onLogin(result: String) {
}
})
获取登录授权后的回调authCode
/**
* Google登录接收SDK在onActivityResult中的回调
*
* @param requestCode
* @param resultCode
* @param data
* */
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
val task = GoogleSignIn.getSignedInAccountFromIntent(data)
try {
val account = task.getResult(ApiException::class.java)
val serverAuthCode = account.serverAuthCode
if (!serverAuthCode.isNullOrEmpty()) {
Toast.makeText(applicationContext, "google登录的结果:" + serverAuthCode, Toast.LENGTH_LONG).show()
Log.i("minfos", "google登录的结果:" + serverAuthCode)
ClassRoomActivity.loginCode = serverAuthCode
startActivity(Intent(this, ClassRoomActivity::class.java))
}
} catch (ae: ApiException) {
Log.i("minfos", "ApiException:" + ae.message)
} catch (e: Exception) {
Log.i("minfos", "Exception:" + e.message)
}
}
7. 打正式签名的Release包,调起Google登录
前提是接入了VPN服务,并且在该设备上安装了GMS服务并已登录Google账号。
7. 请求接口获取google登录的Access Token
接口请求页面地址: https://developers.google.com/identity/sign-in/android/offline-access?hl=zh-cn
接口请求方法
fun getAccesstoken() {
val urlParams: Map<String, Any> = HashMap()
val bodyParams: MutableMap<String, Any> = HashMap()
bodyParams["code"] = authtoken
bodyParams["client_id"] = "xxxx.apps.googleusercontent.com"
bodyParams["cilent_secret"] = "xxxx"
bodyParams["redirect_uri"] = ""
bodyParams["grant_type"] = "authorization_code"
//设置请求头content-type
val contentType = HttpUtil.APPLICATION_X_WWW_FORM_URLENCODED
val url = "https://oauth2.googleapis.com/token"
SendSignHttp.sendRequest(url,
urlParams,
bodyParams,
RequestMethod.POST,
contentType,
object : Callback {
override fun onFailure(call: Call, e: IOException) {
Log.i("minfos", "请求失败:" + e)
}
@Throws(IOException::class)
override fun onResponse(call: Call, response: Response) {
response.body()?.let {
try {
var data = it.string()
Log.i("minfos", "请求的数据access token:" + data)
} catch (e: Exception) {
e.printStackTrace()
}
}
}
})
}
8. 操作效果
8.1 调起用户授权登录:
8.2 展开授权登录项目获取数据的权限范围
参考链接:
https://blog.csdn.net/zhangjianguo1_94/article/details/130342102
https://blog.csdn.net/msn465780/article/details/80765391
https://console.cloud.google.com/apis/credentials/oauthclient