[英]Im failing to authenticate Big query from my node.js firebase functions index.js file
I am using the big query API in my firebase functions index.js file, but I keep getting an internal server error when initializing the Big query instance, my first attempt was to include my service account key file in the functions directory and my code is below我在我的 firebase 函数 index.js 文件中使用了大查询 API,但是在初始化大查询实例时我不断收到内部服务器错误,我的第一次尝试是将我的服务帐户密钥文件包含在函数目录中,我的代码是以下
const functions = require("firebase-functions");
const express = require('express');
const cors = require('cors')({ origin: true });
const app = express();
app.use(cors);
app.use(express.json());
exports.api = functions.https.onRequest(app);
app.post('/create_table', (req, res) => {
'use strict';
const { BigQuery } = require('@google-cloud/bigquery');
const options = {
keyFilename: 'gamelot-c057c-837d4660ae39.json',
projectId: 'gamelot-c057c',
};
const bigquery = new BigQuery(options);
this is the line causing the error这是导致错误的行
const { BigQuery } = require('@google-cloud/bigquery');
const options = {
keyFilename: 'gamelot-c057c-837d4660ae39.json',
projectId: 'gamelot-c057c',
};
const bigquery = new BigQuery(options);
how can I solve this?我该如何解决这个问题?
If you are using Cloud Functions for Firebase (ie Cloud Functions deployed via the Firebase CLI), you should not pass any options when creating the BigQuery client.如果您使用 Cloud Functions for Firebase(即通过 Firebase CLI 部署的 Cloud Functions),则在创建 BigQuery 客户端时不应传递任何选项。 Just do as follows:
只需执行以下操作:
const { BigQuery } = require('@google-cloud/bigquery');
const bigquery = new BigQuery();
As a matter of fact, Cloud Functions for Firebase use the App Engine default service account, ie {project-id}@appspot.gserviceaccount.com
, which has an Editor role , which, by default, contains permissions to interact with BigQuery.事实上,Cloud Functions for Firebase 使用 App Engine 默认服务帐户,即
{project-id}@appspot.gserviceaccount.com
,它具有 Editor角色,默认情况下包含与 BigQuery 交互的权限。
You can check here if the Editor "basic" role has the desired permissions.您可以在此处检查“编辑器”“基本”角色是否具有所需的权限。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.