在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诽俯、設(shè)置你的Android Studio.
必備條件:
1川无、android設(shè)備需要Android 4.1及以上版本呛占,并且安裝Google Play 商店。
或者模擬器Android4.2.2以上版本懦趋,Google Play services 版本15.0.0以上
2晾虑、最新版本的Android SDK,安裝SDK Tools 組件仅叫。并且在Android Studio里面配置好SDK帜篇。
3、項(xiàng)目編譯版本在4.1以上版本诫咱。
以下是具體步驟:
一笙隙、添加 Google Play services
1、在project build.gradle 文件中, 確認(rèn)包含 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'
}
二、配置一個(gè) Google API Console 項(xiàng)目
https://console.developers.google.com/apis/credentials
選擇android項(xiàng)目掏呼。
添入: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 中獲取對(duì)象 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");
}
}