代码编织梦想

参考资料
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

待补充

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 本文链接: https://blog.csdn.net/codecat_yi/article/details/111114518