简体   繁体   English

当我使用服务帐户密钥初始化 firebase 时,部署 firebase 云 function 失败

[英]Deploying firebase cloud function fails when I initialise firebase with a service account key

so very recently I started using google's firebase cloud functions, and loved it immediately, I very quickly restructured a project I was going to work on, and included firebase in it, so I could use the cool features of firestore.所以最近我开始使用谷歌的 firebase 云函数,并立即喜欢上了它,我很快重组了一个我将要从事的项目,并将 firebase 包含在其中,这样我就可以使用 firestore 的酷功能了。 in combination with cloud functions.结合云功能。

Up until today, everything went on smoothly;直到今天,一切都很顺利; pretty much, until I decided to play with google's FCM (Firebase Cloud Messaging) to send notifications via node.js .差不多,直到我决定使用谷歌的 FCM(Firebase 云消息传递)通过node.js发送通知。 Before this, I had created some really dense functions and already deployed to my console which were working seamlessly.在此之前,我已经创建了一些非常密集的功能,并且已经部署到我的控制台上,它们可以无缝运行。 The tricky part is, at the time I created and deployed these functions, I initialised my firebase app in node.js with admin.initalizeApp() .棘手的部分是,在我创建和部署这些函数时,我使用admin.initalizeApp()node.js中初始化了我的 firebase 应用程序。
With this, everything worked fine(both locally & deployed) until I tried to use admin.messaging().sendToDevice...有了这个,一切都很好(本地和部署),直到我尝试使用admin.messaging().sendToDevice...
which resulted in a very nasty error, that basically told me I couldnt send notifications if I wasnt authenticated..这导致了一个非常讨厌的错误,基本上告诉我如果我没有经过身份验证我就无法发送通知..

The error错误

 (Error: An error occurred when trying to authenticate to the FCM servers. Make sure the credential used to authenticate this SDK has the proper permissions. See https://firebase.google.com/docs/admin/setup for setup instructions. Raw server response: "<HTML>
>  <HEAD>
>  <TITLE>Unauthorized</TITLE>
>  </HEAD>
>  <BODY BGCOLOR="#FFFFFF" TEXT="#000000">
>  <H1>Unauthorized</H1>
>  <H2>Error 401</H2>
>  </BODY>
>  </HTML>
>  ". Status code: 401.)

Following the error, I used a few tips from some other users on stack overflow who had faced this error, and most of them suggested that I download a service key from my console, and initialise my firebase app with admin.initializeApp({credential:admin.credential.cert(serviceAccount)})出现错误后,我使用了其他一些遇到此错误的用户关于堆栈溢出的一些提示,其中大多数建议我从控制台下载服务密钥,并使用admin.initializeApp({credential:admin.credential.cert(serviceAccount)})

This solution worked beautifully, as it allowed me to test my notification without seeing the above error ever again .这个解决方案效果很好,因为它允许我测试我的通知,而不会再看到上述错误。
However, when all my tests were done, and I was ready to deploy , the new function I had just created to work on notification, as well as all my previously deployed functions could not get deployed.但是,当我完成所有测试并准备好部署时,我刚刚创建的用于处理通知的新 function 以及我之前部署的所有功能都无法部署。 All of a sudden, the old working functions in my console had a red exclamation mark beside them, and I had to get rid of them.突然之间,我控制台中的旧工作功能旁边出现了一个红色的感叹号,我不得不摆脱它们。 Even after I cleared out all of my console and tried to redeploy all my functions, it failed, and failed and failed with no errors (context: I wasted the whole day,!! lool!) Every tip on the inte.net failed for me, until I reverted back to my old way of initialising my firebase app admin.initializeApp() , then booom!即使在我清除了所有控制台并尝试重新部署我的所有功能之后,它还是失败了,并且失败并且没有错误地失败(上下文:我浪费了一整天,!!哈哈!)inte.net 上的每个提示都失败了我,直到我恢复到初始化我的 firebase 应用程序admin.initializeApp()的旧方法,然后booom! all my functions uploaded successfully, and then again, the authentication error appeared again when I tried to retest my notification function.....我的所有功能都成功上传,然后再次尝试重新测试我的通知时再次出现身份验证错误 function.....

I guess my question is: is there anything I don't know about deploying functions to the firebase console with my app initialised with a service account key I downloaded from my console ?我想我的问题是:关于将功能部署到 firebase 控制台以及使用我从控制台下载的服务帐户密钥初始化我的应用程序,我有什么不知道的吗? Is there something else I need to do to get my functions to deploy properly every time I init my firebase admin app with a service account key??每次我使用服务帐户密钥初始化我的 firebase 管理应用程序时,我还需要做些什么来正确部署我的功能吗? Because initialising the app with just .initalizeApp() works fine for all other purposes both locally and when deployed, except when using FCM .因为仅使用.initalizeApp()初始化应用程序可以很好地用于本地和部署时的所有其他目的,使用FCM时除外。 Can anyone please help with what is happening here??任何人都可以帮助解决这里发生的事情吗?

I think it can be solved by initializing two apps and using them as objects described here .我认为可以通过初始化两个应用程序并将它们用作此处描述的对象来解决。 One with credentials that work for other functions and one for messaging.一种具有用于其他功能的凭据,另一种用于消息传递。

If you need it only for one function you can do it even inside it.如果你只需要一个 function 你甚至可以在里面做。 I have tested it like this:我已经像这样测试过它:

const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp({
  credential: admin.credential.applicationDefault()
});

exports.first_app = functions.https.onRequest(async (req, res) => {
    res.json(admin.app().name);
})

exports.other_app = functions.https.onRequest(async (req, res) => {
    var otherApp = admin.initializeApp({
    credential: **<< different credential here >>**
    }, "2nd_app");
     res.json(otherApp.name);
})

as already mentioned, you should initialize a second app just for the new function you are creating.如前所述,您应该只为您正在创建的新 function 初始化第二个应用程序。 You should put the initialization code inside the new function like this您应该像这样将初始化代码放在新的 function 中

export const saveMap = functions.https.onRequest(async (req, response) => {
    const serviceAccount = require("./../serviceAccountKey.json");
    admin.initializeApp({
        projectId: "serviceAccount.project_id", 
        credential: admin.credential.cert(serviceAccount),
        databaseURL: "https://your_project_id_here.firebaseio.com", //update this
        storageBucket: "your_bucket_name_here.appspot.com" //update this
    }, "2nd_app")

I had the same issue and once I put the second initialization code into the new function, it worked.我遇到了同样的问题,一旦我将第二个初始化代码放入新的 function 中,它就起作用了。 Note that in this code the serviceAccountKey.json is in the same folder as src and lib.请注意,在此代码中,serviceAccountKey.json 与 src 和 lib 位于同一文件夹中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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