试图对FCM服务器进行身份验证时,发生了错误'在Firebase云功能上
我试图在Firbase Cloud函数中使用FCM将消息发送给主题时,当创建Firestore文档(消息)时触发。订阅该主题(也使用函数完成)并触发发送函数效果很好,但是实际发送失败的方法是:
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 HTML,其中包含< h1> project_not_permitten</h1/h1 < h1> project_not_permittit</h1>
。
这是我的代码(index.ts):
import * as admin from 'firebase-admin';
admin.initializeApp({
credential: admin.credential.applicationDefault(),
});
export * from './messages';
and(messages.ts):
import * as admin from 'firebase-admin';
import * as functions from 'firebase-functions';
export const publishMessage = functions
.firestore.document('/messages/{messageId}').onCreate(
(snapshot, context) => {
const data = snapshot.data();
const message = {
notification: {
title: `${data.sentBy} sent a message`,
body: data.message,
},
};
return admin.messaging().sendToTopic('messages', message);
},
);
根据 https://firebase.google.com/docs/cloud-messaging/auth-server#provide-credentials-using-using-adc 这应该有效。我也尝试过没有任何参数( https://firebase.google .com/docs/admin/setup#initialize-without-without-parameters ),但失败了。我想念什么?
I'm trying to send a message to a topic with FCM in a Firebase Cloud Function triggered when a Firestore document (a message) is created. Subscribing to the topic (also done with Functions) and triggering the send function works fine, but the actual send fails with:
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.
and some raw HTML containing <H1>PROJECT_NOT_PERMITTED</H1>
and <H1>PROJECT_NOT_PERMITTED</H1>
.
Here is my code (index.ts):
import * as admin from 'firebase-admin';
admin.initializeApp({
credential: admin.credential.applicationDefault(),
});
export * from './messages';
and (messages.ts):
import * as admin from 'firebase-admin';
import * as functions from 'firebase-functions';
export const publishMessage = functions
.firestore.document('/messages/{messageId}').onCreate(
(snapshot, context) => {
const data = snapshot.data();
const message = {
notification: {
title: `${data.sentBy} sent a message`,
body: data.message,
},
};
return admin.messaging().sendToTopic('messages', message);
},
);
According to https://firebase.google.com/docs/cloud-messaging/auth-server#provide-credentials-using-adc this should work. I have also tried doing it without any parameters (https://firebase.google.com/docs/admin/setup#initialize-without-parameters) but it fails all the same. What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
事实证明,
我不需要这个:
Turns out that instead of this
I needed this:
您可以在项目设置中启用云消息传递API(遗产)/云消息
https://i.sstatic。 net/elwpb.png
you can do enable Cloud Messaging API (Legacy) in Project settings/Cloud Messaging
https://i.sstatic.net/ElwpB.png
这是因为Google Firebase版本更改了,
admin.messaging()。sendTodeVice()
不再盒子。现在以下内容效果很好:
Note
Databaseurl
不再需要Firebase-Admin中的数据库。只需在Firebase控制台中下载的私钥即可。This is because Google Firebase versions changed and
admin.messaging().sendToDevice()
no longer works out of the box.Now following works well:
Note
databaseURL
in firebase-admin is no longer needed. Just private key you download in Firebase Console.就我而言,使API解决了问题。默认似乎仅
Firebase Cloud Messaging
API已启用,但cloud Messaging
未启用API。您需要从 apis page 中启用该问题,它将解决该问题In my case, enabling API solved the issue. Seems like by default only
Firebase Cloud Messaging
API is enabled butCloud Messaging
API is not enabled. You need to enable that one from APIs page and it will solve the issue角色/firebase.admin )之后
在我的
&lt; projectId&gt;@appspot.gserviceaccount.com
中删除(//i.sstatic.net/egyoe.png“ rel =“ nofollow noreferrer”>
由于
&lt; projectId&gt;@appspot.gserviceaccount.com
服务帐户正在使用Firebase Cloud功能。添加到此服务帐户的角色和权限将其延续到云功能运行时。如您已经说过的那样,您具有
&lt; projectId&gtspot.gserviceaccount.com
,请确保它具有(角色/firebase.admin
),并且对此文档,它具有cloudMessaging。*权限。
在重新添加
&lt; projectId&gt;@appspot.gserviceaccount.com
和conerspoding(roles/firebase.admin
):i.sstatic.net/pshkx.png“ rel =“ nofollow 've尝试添加另一个firestore文档来测试该功能(10分钟后),以确保我的权限的更改生效:
对于其他选项,您也可以尝试添加:
角色/firebase.growthadmin
);角色/FireBasecloudMessaging.admin
),因为它也具有cloudMessaging。*
允许。您还可以检查其他角色具有相应的权限。
After removing the (
roles/firebase.admin
) in my<projectId>@appspot.gserviceaccount.com
, I also encountered your error:Since the
<projectId>@appspot.gserviceaccount.com
service account is being used by Firebase Cloud Functions. Roles and permissions added to this service account carry over to the Cloud Functions runtime.As you've already stated, you have
<projectId>@appspot.gserviceaccount.com
, make sure that it has a (roles/firebase.admin
), and according to this documentation, it has a full access forcloudmessaging.*
permission.After I re-added the
<projectId>@appspot.gserviceaccount.com
and the correspoding (roles/firebase.admin
):I've tried adding another Firestore document to test the function (after 10 minutes or so) just to make sure that the changes in my permissions have taken effect:
For other options, you can also try to add the:
roles/firebase.growthAdmin
);roles/firebasecloudmessaging.admin
) since it also hascloudmessaging.*
permission.You can also check the other roles with it's corresponding permissions.