【问题标题】:How to import firebase into google apps script, in order to create users with google provider?如何将firebase导入谷歌应用程序脚本,以便使用谷歌提供商创建用户?
【发布时间】:2019-04-04 04:00:49
【问题描述】:

我正在尝试通过 google auth 提供程序自动或通过单击 google 表格中侧边栏插件中的按钮在 firebase 中创建一个新用户。原因是我需要为用户获取注册的 uid,以便我从工作表发送的数据可以归因于该用户。

我找到的最接近教程的是this,但我在尝试导入 firebase 时卡住了。

我经常看到的一种方法是通过 html 中的脚本标签将其导入工作表侧栏。

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <script>
      // Initialize Firebase
      var config = {
        apiKey: '...,
        authDomain: '...',
        databaseURL: '...',
        projectId: '...',
        storageBucket: '...',
        messagingSenderId: '...'
      };
      firebase.initializeApp(config);
    </script>
</body>
</html>

但是,当我尝试在 .gs 脚本中使用 firebase 变量时,它是未定义的。我如何使它对脚本可用?

我见过的另一种方法是在 gs 脚本中使用 evalURLFetchApp

eval(UrlFetchApp.fetch("https://www.gstatic.com/firebasejs/5.9.2/firebase.js").getContentText());

这导致SyntaxError: Missing name after . operator. 我认为这是因为该库不打算在节点 js 上运行。

我意识到 firebase 的 admin sdk 是为节点 js 设计的,但您无法使用此方法 (afaik) 创建具有特定提供程序的用户。另外,我不确定如何将其导入 Google 应用程序脚本。此外,我希望用户在启动创建用户过程后与 google 帐户选择器进行交互。

有人对如何完成这项工作有任何建议吗?

【问题讨论】:

    标签: firebase google-apps-script firebase-authentication


    【解决方案1】:

    实际上,使用 Firebase Auth REST API 从 Google 表格在 Firebase 中创建新用户非常容易。

    您必须向 Auth signupNewUser 端点发出 HTTP POST 请求,请参阅此处的详细文档:https://firebase.google.com/docs/reference/rest/auth/#section-create-email-password

    为此,您将在 gs 脚本中使用 URLFetchApp() 类的 fetch() 方法,如下所示:

    function createUser() {
    
      const userName = "john.doe@gmail.com";
      const pwd = "xyz987";
    
      const createUserUrl = "https://www.googleapis.com/identitytoolkit/v3/relyingparty/signupNewUser?key=[API_KEY]" //Replace with your Web API Key
    
      const payload = JSON.stringify({"email": userName, "password": pwd, "returnSecureToken": true});
    
      const createUserResponse = UrlFetchApp.fetch(createUserUrl, {
            method: 'post',
            contentType: 'application/json',
            muteHttpExceptions: true,
            payload : payload
     });
    
     Logger.log(JSON.parse(createUserResponse));
    
    }
    

    您将通过 Firebase 管理控制台中的项目设置页面获取 Firebase 项目的 Web API 密钥。

    【讨论】:

    • 有趣,您能否对此进行扩展以允许用户通过他们的 google 帐户(google auth 提供者)创建帐户?如果我可以通过其他 API 来完成这项工作,那么我想我就可以上路了!
    • 实际上这看起来像我想要的 - firebase.google.com/docs/reference/rest/auth/… 这样的东西应该可以正常工作吗? curl 'https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyAssertion?key=[API_KEY]' \ -H 'Content-Type: application/json' \ --data-binary '{"postBody":"id_token=[GOOGLE_ID_TOKEN]&amp;providerId=[google.com]","requestUri":"[http://localhost]","returnIdpCredential":true,"returnSecureToken":true}'
    • @rt_ 我不知道,我从未尝试过这个端点。我明天也许可以看看,对我来说已经很晚了,实际上我要睡觉了!但我相信你会找到文档的方法。
    • 好的 NP,我快让它工作了,所以不用担心。我将上述方法与官方应用脚本 OAuth2 库一起使用。谢谢雷诺!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多