【问题标题】:How to manage tenants programatically in Google Cloud Platform如何在 Google Cloud Platform 中以编程方式管理租户
【发布时间】:2022-01-14 04:40:33
【问题描述】:

我正在编写一个后端服务来管理我在 gcp 中的租户。具体来说,我希望能够在我的节点服务器上创建/删除和列出租户。

Firebase admin-sdk 应该能让我这样做。当我尝试运行它时,出现此错误:

Credential implementation provided to initializeApp() via the "credential" property failed to fetch a valid Google OAuth2 access token with the following error: "Error fetching access token: Error while making request: getaddrinfo ENOTFOUND metadata.google.internal. Error code: ENOTFOUND".

我按照this documentation 设置安装管理sdk。 (尝试了windows和linux,使用环境变量) 我用this documentation(获取现有租户)

这是我的代码:

var admin = require('firebase-admin');
var app = admin.initializeApp({
    credential: admin.credential.applicationDefault(),
    projectId: 'myProject'
});
admin.auth().tenantManager().getTenant("myTenant")
    .then((tenant) => {
        console.log(tenant.toJSON());
    })
    .catch((error) => {
        // Handle error.
        console.log(error.message)
    });


const someOtherStuff = () =>...

module.exports = {
    someOtherStuff
}

编辑:我正在使用 Express 在节点服务器上本地运行此代码。我正在使用 Windows 计算机和 Linux 计算机。结果在两个系统上是相同的。

【问题讨论】:

  • 你在什么上面运行这段代码?元数据服务仅在计算服务(Compute Engine、Cloud Run 等)上运行时可用。使用详细信息编辑您的问题。

标签: node.js google-cloud-platform multi-tenant firebase-admin


【解决方案1】:

我能够通过更改初始化来解决这个问题。我没有使用环境变量,而是直接使用了服务帐户密钥文件,如 here 所述

我如何使用它的一些示例代码:

var admin = require('firebase-admin');
var {getAuth} = require('firebase-admin/auth');
var serviceAccount = require('/path/to/serviceAccountKey.json');

// Initialize the default app using seriveAccount instead of environment variables
var app = admin.initializeApp({
    credential: admin.credential.cert(serviceAccount)
});
   

const createTenant = async (tenantName) => getAuth(app).tenantManager().createTenant({
    displayName: tenantName,
    emailSignInConfig: {
        enabled: true,
        passwordRequired: true, // Email link sign-in enabled.
    }
}).then((createdTenant) => {
    return createdTenant.toJSON();
}).catch((error) => {
    console.log("tenant could not be created. " + error.message);
});

//some other stuff...

module.exports = {
    createTenant,
    someOtherStuff,
}

【讨论】:

    猜你喜欢
    • 2018-02-18
    • 1970-01-01
    • 2020-10-06
    • 2021-11-28
    • 2021-10-20
    • 2020-08-22
    • 1970-01-01
    • 2017-09-17
    • 2021-09-30
    相关资源
    最近更新 更多