一、接入准备
- 手机必须有Google套件
Google服务框架,Google Play商店,Google Play服务(使用su谷歌安装器下载安装) - 手机必须可以翻墙
- google开发者账号
二、官网文档
https://developers.google.com/identity/sign-in/android/start-integrating?hl=zh-cn
三、接入步骤
- 依赖google play服务
implementation 'com.google.android.gms:play-services-auth:20.2.0'
-
配置 Google API 控制台项目
需要使用google开发者账号,创建项目,在项目中根据包名和SHA1值创建应用。
控制台: https://console.cloud.google.com/apis/credentials?hl=zh-cn -
代码
3.1 获取GoogleSignInClient 对象
private final String TAG = getClass().getSimpleName();
/**
* google登录客户端对象
*/
private GoogleSignInClient mGoogleSignInClient;
private void initGoogleSignInClient() {
// Configure sign-in to request the user's ID, email address, and basic
// profile. ID and basic profile are included in DEFAULT_SIGN_IN.
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
//请求邮箱
.requestEmail()
//请求id token(web client id)
.requestIdToken("660809532020-g7r2qsmjkjv0ttmje013otrb97s0qtil.apps.googleusercontent.com")
.build();
// Build a GoogleSignInClient with the options specified by gso.
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
}
3.2 登录
/**
* google登录
*/
private void login() {
//使用GoogleSignIn.getLastSignedInAccount方法为当前登录的用户请求个人资料信息。
GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
//不存在则请求登录
if (account == null) {
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
mActivityLauncher.launch(signInIntent);
} else {
showAccount(account);
}
}
private final ActivityResultLauncher<Intent> mActivityLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
if (result.getResultCode() == RESULT_OK) {
//回调成功
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(result.getData());
handleSignInResult(task);
}
});
/**
* 处理数据
*
* @param completedTask
*/
private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
try {
GoogleSignInAccount account = completedTask.getResult(ApiException.class);
// Signed in successfully, show authenticated UI.
showAccount(account);
} catch (ApiException e) {
// The ApiException status code indicates the detailed failure reason.
// Please refer to the GoogleSignInStatusCodes class reference for more information.
Log.e(TAG, "signInResult:failed code=" + e.getStatusCode());
Log.e(TAG, "signInResult:failed msg=" + e.getStatusMessage());
}
}
/**
* 展示账户信息
*
* @param account
*/
private void showAccount(GoogleSignInAccount account) {
Log.e(TAG, "signInResult:success name:" + account.getDisplayName());
Log.e(TAG, "signInResult:success id:" + account.getId());
Log.e(TAG, "signInResult:success email:" + account.getEmail());
Log.e(TAG, "signInResult:success token:" + account.getIdToken());
}
3.3 登出
/**
* 登出
*/
private void logout() {
mGoogleSignInClient.signOut()
.addOnCompleteListener(this, new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
Log.e(TAG, "退出登录");
}
});
}
3.4 断开账户
/**
* 断开账户,撤销访问(如果用户删除其帐户,则必须删除您的应用程序从Google API获取的信息)。
*/
private void revokeAccess() {
mGoogleSignInClient.revokeAccess()
.addOnCompleteListener(this, new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
Log.e(TAG, "断开账户");
}
});
}