Flutter实现google登陆(gmail)

参考资料
Flutter Google Sign In

环境介绍

系统:Windows 10
IDE:Android Studio 3.5.2
Flutter:1.22.2
Dart:2.8.4
Gradle:5.6.2

插件

  1. google_sign_in
  2. firebase_auth

平台

Android

  1. 前往Firebase创建项目
  2. 侧边栏进入build -> Authentication -> Sign-in method,开启Google项
  3. 侧边栏进入 Project Overview ,创建APP
  4. 进入APP设置,填写基本信息
  5. 使用keytool创建keystore,并获取SHA-1、SHA-256,并填写到对应APP信息中
  6. 下载google-services.json放入android/app/
  7. 配置OAuth同意屏
  8. 配置android/build.gradle
    buildscript{
    	...
    	repositories{
        	maven { url 'https://maven.aliyun.com/repository/google' }
        	maven { url 'https://maven.aliyun.com/repository/jcenter' }
        	maven { url 'http://maven.aliyun.com/nexus/content/groups/public' }
    	}
    	dependencies{
    		...
        	classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        	classpath 'com.google.gms:google-services:4.3.3'
    	}
    }
    ...
    allprojects{
    	repositories{
        	maven { url 'https://maven.aliyun.com/repository/google' }
        	maven { url 'https://maven.aliyun.com/repository/jcenter' }
        	maven { url 'http://maven.aliyun.com/nexus/content/groups/public' }
    	}
    }
    
  9. 配置android/app/build.gradle
    dependencies{
        implementation 'com.google.firebase:firebase-analytics:17.2.2'
    }
    apply plugin: 'com.google.gms.google-services'
    
  10. 编写代码
    // 要求的 google token scope
    final GoogleSignIn _googleSignIn = GoogleSignIn(
      scopes: <String>[
        'email',
        'profile',
      ],
    );
    // 谷歌登陆
    Future _loginForGoogle(BuildContext context) async {
      await _googleSignIn.signOut();
    
      GoogleSignInAccount _currentUser = await _googleSignIn.signIn();
      GoogleSignInAuthentication gSA = await _currentUser.authentication;
    
      var id = _currentUser.id;
    
      // 需要发送给后端的accessToken
      String accessToken = gSA.accessToken;
      
      // ...继续操作
    }
    

IOS

待补充

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值