注意事项:
1.手机必须有google套件
2.手机必须翻墙
集成就比较简单了,安装google官方文档集成即可。
集成网址:https://console.firebase.google.com/?hl=zh-cn
配置requestIdToken :https://console.developers.google.com/apis/credentials
官方集成文档:https://developers.google.com/identity/sign-in/android/sign-in?hl=zh-cn
注册成功后,要把google-services.json复制到app目录下,google-services.json里面的
requestIdToken:在google-services.json里面和配置requestIdToken网址里面应该一样。
打包测试或正式环境 .jks 或 keystory 签名文件要和google注册的要使用一样的。
在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" );
}
}