Android 接入Google登入
阿新 • • 發佈:2018-12-19
注意事項:
1.手機必須有google套件
2.手機最好安裝上google pay 已測(8.1手機非必須)
3.手機必須翻牆
整合就比較簡單了,安裝google官方文件整合即可。
註冊成功後,要把google-services.json複製到app目錄下,google-services.json裡面的
requestIdToken:在google-services.json裡面和配置requestIdToken網址裡面應該一樣。
在project目錄下
// Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { repositories { google() mavenCentral() jcenter() } dependencies { classpath 'com.google.gms:google-services:4.1.0' } } allprojects { repositories { google() mavenCentral() jcenter() // maven { url "https://maven.google.com" } } }
在app:
apply plugin: 'com.android.application' apply plugin: 'com.google.gms.google-services' android { compileSdkVersion 27 defaultConfig { applicationId "com.lxp.test.goolelogin" minSdkVersion 15 targetSdkVersion 27 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } signingConfigs { releaseConfig { try { keyAlias 'jvv' keyPassword '123456' storeFile file('../app/jvv.keystore') storePassword '123456' } catch (ex) { throw new InvalidUserDataException(ex.toString()) } } debugConfig { try { keyAlias 'jvv' keyPassword '123456' storeFile file('../app/jvv.keystore') storePassword '123456' } catch (ex) { throw new InvalidUserDataException(ex.toString()) } } } buildTypes { release { signingConfig signingConfigs.releaseConfig minifyEnabled false shrinkResources false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } debug { signingConfig signingConfigs.releaseConfig minifyEnabled false // signingConfig signingConfigs.debug proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:27.1.1' implementation 'com.google.firebase:firebase-core:16.0.4' implementation 'com.google.firebase:firebase-auth:16.0.4' implementation 'com.google.android.gms:play-services-auth:15.0.1' }
程式碼裡面:
GoogleSignInOptions gso; GoogleSignInClient googleSignInClient; private void login(){ gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) .requestIdToken(getString(R.string.app_google_id)) .requestEmail() .requestId() .requestProfile() .build(); googleSignInClient = GoogleSignIn.getClient(this, gso); }
登入時呼叫:
textLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = googleSignInClient.getSignInIntent();
startActivityForResult(intent,requestLoginCode);
}
});
回撥資料:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == requestLoginCode){
Task<GoogleSignInAccount> signedInAccountFromIntent = GoogleSignIn.getSignedInAccountFromIntent(data);
handleResult(signedInAccountFromIntent);
}
}
private void handleResult( Task<GoogleSignInAccount> googleData) {
try {
GoogleSignInAccount signInAccount = googleData.getResult(ApiException.class);
GoogleSignInAccount acct = GoogleSignIn.getLastSignedInAccount(this);
if (signInAccount != null && index == 0) {
Log.e("account", "si:" + "\n" + signInAccount.getEmail());
String str = signInAccount.getEmail()+"\n"
+signInAccount.getId()+"\n"+
signInAccount.getAccount().name+"\n"+
signInAccount.getDisplayName()+"\n"+
signInAccount.getGivenName()+"\n";
textView.setText(str);
}else {
Log.e("account", "si為空:" + "\n" );
}
}catch (Exception e){
e.printStackTrace();
Log.e("account", "si異常:" + "\n" );
}
}
需要注意 .jks 或 keystory 簽名檔案要和google註冊的要使用一樣的。