【问题标题】:Accessing Auth from Firebase Cloud Functions从 Firebase Cloud Functions 访问 Auth
【发布时间】:2018-11-23 21:13:44
【问题描述】:

我无法从 Firebase Cloud Functions 环境访问用户信息。

我想做什么。

const admin = require('firebase-admin') 
admin.initializeApp() 
exports.test = functions.https.onRequest(() => {   
    admin.auth().getUser('<user_id>').then(user => {
        return console.log(user)   
    }).catch(e => {
        console.error(e)   
    })
 })

现在,当我运行 test 函数时,我收到以下错误。

{ code: 'auth/insufficient-permission',
 message: 'Credential implementation provided to initializeApp() via the "credential" property has insufficient permission to access the requested resource. See https://firebase.google.com/docs/admin/setup for details on how to authenticate this SDK with appropriate permissions.' }, 
 codePrefix: 'auth' }

现在,我的问题是,是否可以在云函数环境中使用 admin.auth().getUser

【问题讨论】:

    标签: node.js firebase firebase-authentication google-cloud-functions


    【解决方案1】:

    您需要将凭证文件传递给 admin sdk 初始化函数。认证相关任务在云函数中需要此配置。

    转到 Firebase Console -> 设置 -> 项目设置 -> 服务帐户 -> 生成新的私钥

    下载文件并包含在您的代码中。

    P.S:确保此文件不以任何形式公开(将其添加到 git ignore),因为拥有此文件可以让您完全访问 firebase 项目。

    【讨论】:

    • 或者您可以将Service Account Token Creator 添加到您的 IAM 凭证中,firebase 功能将自动成为一个安全的环境
    • 看不到这是如何回答问题的。我在使用服务帐户时收到此错误。
    猜你喜欢
    • 2020-02-01
    • 2017-11-03
    • 2023-03-11
    • 2017-08-08
    • 2017-10-17
    • 2023-04-06
    • 1970-01-01
    • 2020-09-01
    • 2017-08-24
    相关资源
    最近更新 更多