nativeScript-google-signin
A NativeScript 模块,为 Android 提供 Google 登录功能
npm i --save nativescript-google-signin

nativeScript-google-signin

用于使用 Google 登录的 NativeScript 应用程序的插件

先决条件

您需要将配置文件 "google-services.json" 复制到 platforms/android/ 目录,并设置 Google 的先决条件 必须设置

安装

  • 添加插件 tns plugin add nativescript-google-signin

  • 将您的 auth 或请求代码添加到 strings.xml(platforms/android/src/main/res/values/strings.xml)中

    <string name="google_auth_code">your-auth-code</string>

    或者

    <string name="google_request_code">your-request-code</string>

    您还可以使用配置对象传递此代码。

  • 在您的 build.gradle(platforms/android/build.gradle)中

    • 在 buildscript 块中添加 mavenCentral()classpath 'com.google.gms:google-services:3.0.0'
    buildscript {
    repositories {
    jcenter()
    mavenCentral()
    }

    dependencies {
    classpath "com.android.tools.build:gradle:2.1.2"
    classpath 'com.google.gms:google-services:3.0.0'
    }
    }
    • apply plugin: "com.android.application" 下方添加 apply plugin: 'com.google.gms.google-services'

用法

var googleSignIn = require("nativescript-google-signin");

创建一个配置对象

var config = {
authCode: "your-auth-code",
requestProfile: true
};
  • authCode:请求服务器授权的代码
  • requestToken:请求访问令牌的代码(如果 authCode 被跳过,则需要设置)
  • requestProfile:如果您想获取配置文件信息,则设置为 true(如果跳过,则只请求电子邮件信息)

如果没有在 xml 文件中创建字符串变量,则必须设置 authCode 或 requestToken

作为第二个参数,您需要传递回调函数,用于处理成功或失败事件

var callbacks = {

onSuccess: function(result){
},

onFailed: function(e){
}

};
  • onSuccess:登录过程成功时调用,参数 "result" 是 com.google.android.gms.auth.api.signin.GoogleSignInAccount 对象 查看
  • onFailed:登录过程中发生问题时调用,参数 "e" 是异常或包含 Google 错误代码的消息。

使用创建的配置对象调用 singIn

googleSignIn.singIn(config, callbacks);

注意

  • 目前该插件仅支持 Android 版本;iOS 将在将来支持(也许有人能帮助我:sweat_smile:)