简体   繁体   English

Firebase云功能触发FCM,但请求缺少必需的身份验证凭据

[英]Firebase cloud function triggers FCM but Request is missing required authentication credential

I am trying to use firebase-admin inside a firebase cloud function to send a message via firebase cloud messaging (FCM). 我正在尝试在firebase云功能中使用firebase-admin通过Firebase云消息传递(FCM)发送消息。

When reading through the documentation it says 阅读文档时说

To use the Admin FCM API, you must first follow the steps in Add the Firebase Admin SDK to your Server. 要使用Admin FCM API,您必须首先按照将Firebase Admin SDK添加到服务器中的步骤进行操作。

But I think this is not required since I'm only using cloud functions? 但是我认为这不是必需的,因为我仅使用云功能吗?

Anyway, it all works up until the point of admin.messaging().send where I get this error: 无论如何,这一切都可以进行到admin.messaging().send到出现此错误的位置:

Error sending message: { Error: Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.
    at FirebaseMessagingError.Error (native)
    at FirebaseMessagingError.FirebaseError [as constructor] (/user_code/node_modules/firebase-admin/lib/utils/error.js:39:28)
    at FirebaseMessagingError.PrefixedFirebaseError [as constructor] (/user_code/node_modules/firebase-admin/lib/utils/error.js:85:28)
    at new FirebaseMessagingError (/user_code/node_modules/firebase-admin/lib/utils/error.js:241:16)
    at Function.FirebaseMessagingError.fromServerError (/user_code/node_modules/firebase-admin/lib/utils/error.js:271:16)
    at FirebaseMessagingRequestHandler.handleHttpError (/user_code/node_modules/firebase-admin/lib/messaging/messaging-api-request.js:125:50)
    at /user_code/node_modules/firebase-admin/lib/messaging/messaging-api-request.js:113:23
    at process._tickDomainCallback (internal/process/next_tick.js:135:7)
  errorInfo: 
   { code: 'messaging/invalid-apns-credentials',
     message: 'Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.' },
  codePrefix: 'messaging' }

This is my source code of the cloud function 这是我的云功能的源代码

import * as functions from 'firebase-functions'
import * as admin from 'firebase-admin'

admin.initializeApp(functions.config().firebase)
const firestore = admin.firestore()
firestore.settings({timestampsInSnapshots: true})

exports.notification = functions.firestore
  .document(path)
  .onUpdate(async (change, context) => {
    const deviceTokens = ['deviceToken-123123123']
    deviceTokens.forEach(token => {
      const fcmMessage = {
        notification: {title: 'test title', body: 'test body'},
        token
      }
      admin.messaging().send(fcmMessage)
        .then((response) => {
          // Response is a message ID string.
          console.log('Successfully sent message:', response)
        })
        .catch((error) => {
          console.log('Error sending message:', error)
        })
    })
  })

The device tokens are stored in firestore and retrieved from firestore inside this cloud function as well. 设备令牌存储在Firestore中,并且也可以从该Cloud函数内部的Firestore中检索。 The format of the device token is correct. 设备令牌的格式正确。 I have replaced it with a placeholder for this example. 在此示例中,我已将其替换为占位符。

I also looked around for similar questions, but the only one I could find was this one 我也到处寻找类似的问题,但我唯一能找到的就是这个

After much searching I found an error list by firebase and on this list I traced the error code 'messaging/invalid-apns-credentials' which said: 经过大量搜索后,我发现了一个由firebase提供的错误列表,并在此列表上跟踪了错误代码'messaging/invalid-apns-credentials' ,其中显示:

A message targeted to an iOS device could not be sent because the required APNs SSL certificate was not uploaded or has expired. 无法发送针对iOS设备的消息,因为所需的APNs SSL证书未上传或已过期。 Check the validity of your development and production certificates. 检查您的开发和生产证书的有效性。

So I figured, I haven't set up my "production" certificate yet, and maybe my cordova dev build is being treated as production build! 所以我想,我还没有建立我的“生产”证书,也许我的cordova开发版本被视为生产版本! So I just went ahead and added the production certificate, also deleted and re-added the dev certificate to be sure, and it worked. 因此,我只是继续添加了生产证书,还确定要删除并重新添加dev证书,并且它可以正常工作。

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

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