【问题标题】:Firebase functions run in one firebase project but giving internal error in the otherFirebase 函数在一个 firebase 项目中运行,但在另一个项目中出现内部错误
【发布时间】:2021-07-05 07:15:00
【问题描述】:

我有两个 firebase 帐户,一个用于开发 (D),另一个用于生产 (P)。我的 development(D) firestore 和函数在 us-central1 上运行。在生产(P)上,firestore 位置是 asia-south1,功能在 us-central1 上运行

我的 firebase 函数在开发 (D) 中运行正常,但在生产中出现以下错误。此外,当我检查 firebase 函数控制台上的日志时,似乎没有任何活动。看起来好像该函数没有被调用。

firebase 函数返回的错误是:

函数调用错误 Fri Apr 09 2021 09:25:32 GMT+0530 (India Standard Time)with{"code":"internal"}

此外,客户端也显示此消息:

CORS 政策已阻止从源“https://setmytest.com”访问“https://us-central1-xxx.cloudfunctions.net/gpublish”获取:对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。如果不透明的响应满足您的需求,请将请求的模式设置为“no-cors”以获取禁用 CORS 的资源。 zone-evergreen.js:1052 POST https://us-central1-xxx.cloudfunctions.net/gpublish net::ERR_FAILED

这是我的 Angular 应用程序调用该函数的代码 -

const process = this.fns.httpsCallable("gpublish");
process(data).subscribe(
  (result) => {
    console.log("function responded with result: " + JSON.stringify(result));
  },
  (err) => {
    const date1 = new Date();
    console.log("Function call error " + date1.toString() + "with" + JSON.stringify(err));
  });

这里是函数-

index.ts

import { gpublish } from "./gpublish/gpublish";
import { sendEmail } from "./sendEmail";

export {gpublish,sendEmail };

gpublish.ts

import * as functions from "firebase-functions";
const fs = require("fs");
const { google } = require("googleapis");
const script = google.script("v1");
const scriptId = "SCRIPT_ID";
const googleAuth = require("google-auth-library");
import { admin } from "../admin";
const db = admin.firestore();

export const gpublish = functions.https.onCall(async (data: any, res: any) => {
  try {
    const googleTest = data.test;
    console.log("Publishing to google test of name " + googleTest.testName);
    
    // read the credentials and construct the oauth client
    const content = await fs.readFileSync("gapi_credentials.json");
    const credentials = JSON.parse(content); // load the credentials
    const { client_secret, client_id, redirect_uris } = credentials.web;
    const functionsOauth2Client = new googleAuth.OAuth2Client(client_id,client_secret, redirect_uris); // Constuct an auth client
    functionsOauth2Client.setCredentials({refresh_token: credentials.refresh_token}); // Authorize a client with credentials

    // run the script
    return runScript(functionsOauth2Client,scriptId,JSON.stringify(googleTest)
    ).then((scriptData: any) => {
      console.log("Script data is" + JSON.stringify(scriptData));
      sendEmail(googleTest, scriptData);
      return JSON.stringify(scriptData);
    });
  } catch (err) {
    return JSON.stringify(err);
  }
});

function runScript(auth: any, scriptid: string, test: any) {
  return new Promise(function (resolve, reject) {
    script.scripts
      .run({auth: auth,scriptId: scriptid, resource: {function: "doGet", devMode: true,parameters: test }
      })
      .then((respons: any) => { resolve(respons.data);})
      .catch((error: any) => {reject(error);});
  });
}

在开发和生产中部署功能时,我已正确更改了服务帐户密钥和 Google 凭据。

我尝试了很多方法,包括以下内容:

该功能在 Development firebase 项目中运行良好,但在 Production firebase 项目中却没有。请帮忙!

【问题讨论】:

  • 我发现在为该功能设置未经身份验证的访问时,它可以完美运行。如何确保 httpsoncall 函数可以通过身份验证运行?请帮忙
  • Firebase 函数必须保持“公开”状态。 allAuthenticatedUsers 选项仅适用于已登录的 Google 帐户(因为它们实际上是 Google Cloud Functions),不适用于 Firebase 身份验证帐户。您需要使用context.auth 拒绝访问您的功能。 more info
  • 谢谢,我担心授予 AllUsers 访问权限,但我现在已经实现了。它有效!

标签: firebase google-cloud-functions


【解决方案1】:

您需要检查您的函数是否已正确部署。

一个不存在的函数 (404 Not Found) 或一个无法访问的函数 (403 Forbidden) 都会出现该错误,因为 Firebase 函数永远不会执行,这意味着正确的 CORS 标头永远不会发回给客户。

【讨论】:

  • 谢谢,这有助于理解firebase返回的“internal”错误信息。
猜你喜欢
  • 2021-04-13
  • 1970-01-01
  • 1970-01-01
  • 2020-11-23
  • 2016-12-21
  • 2018-04-22
  • 1970-01-01
  • 1970-01-01
  • 2020-06-10
相关资源
最近更新 更多