简体   繁体   中英

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

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. 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.

You can check here if the Editor "basic" role has the desired permissions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM