Android 接入Google登录

注意事项:

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" );
        }

    }

 

 

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值