在Android App中集成Google Sign-In。
参考链接:https://developers.google.com/identity/sign-in/android/start-integrating
Google API Console :https://console.developers.google.com
在开始集成之前:
1、你需要配置Google API Console.
2、设置你的Android Studio.
必备条件:
1、android设备需要Android 4.1及以上版本,并且安装Google Play 商店。
或者模拟器Android4.2.2以上版本,Google Play services 版本15.0.0以上
2、最新版本的Android SDK,安装SDK Tools 组件。并且在Android Studio里面配置好SDK。
3、项目编译版本在4.1以上版本。
以下是具体步骤:
一、添加 Google Play services
1、在project build.gradle 文件中, 确认包含 Google's 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'
// }
}
}
2、在app-level build.gradle 文件中, 定义 Google Play services :
apply plugin: 'com.android.application'
...
dependencies {
implementation 'com.google.android.gms:play-services-auth:17.0.0'
}
二、配置一个 Google API Console 项目
https://console.developers.google.com/apis/credentials
选择android项目。
添入:SHA1值。
获取SHA1 keytool -keystore shooting.keystore -list -v
三、添加代码
1、在onCreate中添加:
// 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()
.build();
// Build a GoogleSignInClient with the options specified by gso.
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
2、添加cocos可以访问的登录方法:
/////google signin////////
public static void signInByGoogle() {
Intent signInIntent = AppActivity._instance.mGoogleSignInClient.getSignInIntent();
AppActivity._instance.startActivityForResult(signInIntent, RC_SIGN_IN);
}
////google sign-in
//在onActivityResult 中获取对象 GoogleSignInAccount
//After the user signs in, you can get a GoogleSignInAccount object for the user in the activity's onActivityResult method.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//facebook
callbackManager.onActivityResult(requestCode, resultCode, data);
super.onActivityResult(requestCode, resultCode, data);
SDKWrapper.getInstance().onActivityResult(requestCode, resultCode, data);
//google sign-in
// Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
// The Task returned from this call is always completed, no need to attach
// a listener.
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
handleSignInResult(task);
}
}
////google sign-in
//The GoogleSignInAccount object contains information about the signed-in user, such as the user's name.
private void handleSignInResult(@NonNull Task<GoogleSignInAccount> completedTask) {
Log.d(TAG, "handleSignInResult:" + completedTask.isSuccessful());
try {
GoogleSignInAccount account = completedTask.getResult(ApiException.class);
updateUI(account);
} catch (ApiException e) {
Log.w(TAG, "handleSignInResult:error", e);
// Clear the local account
mAccount = null;
// Signed out, show unauthenticated UI.
updateUI(null);
}
}
////google sign-in
// 更新用户信息
void updateUI(GoogleSignInAccount account){
if(account != null){
// Store the account from the result
mAccount = account;
Log.d(TAG, "mAccount:" + mAccount);
AppActivity._instance.runOnGLThread(new Runnable() {
public void run(){
Cocos2dxJavascriptJavaBridge.evalString("yxl.loginSuccess(\"google"
+ AppActivity._instance.mAccount.getId()
+ "\",\""
+ AppActivity._instance.mAccount.getDisplayName()
+ "\",\""
+AppActivity._instance.mAccount.getPhotoUrl()
+ "\",\""
+AppActivity._instance.displayCountryName
+"\");");
}
});
}else{
Log.d(TAG, "mAccount:null");
}
}