Firebase 云消息:请求包含无效参数



我正在按照本教程进行操作,我已经到了向 fcm 端点发送 HTTP 请求的要点,但我收到以下错误:

{
error:
{
code: 400,
message: 'Request contains an invalid argument.',
status: 'INVALID_ARGUMENT'
}
}

我没有使用 curl 发送请求,而是使用 node 的云函数.js | 使用以下代码表达:

exports.messages = function (req, res) {
const api = 'https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send';
const headers = {
'Accept': 'application/json',
'Content-type': 'application/json',
};
return getAccessToken().then((accessToken) => {
headers.Authorization = `Bearer ${accessToken}` // <-- accessToken is OK
const { title, body, token } = req.body;
return fetch(api, {
headers,
method: 'POST',
body: JSON.stringify(
{
'message': {
'token': token, // <-- this is the client fcm token
'notification': {
'title': title,
'body': body
}
}
}
)
}).then(res => res.json()).then(data => {
console.log(data);
res.sendStatus(200);
}).catch((error) => {
console.error(error);
});
});
}

令牌是我的服务帐户的 OAuth 2 令牌

function getAccessToken() {
return new Promise(function (resolve, reject) {
var key = require('../keys/service-account.json');
var jwtClient = new google.auth.JWT(
key.client_email,
null,
key.private_key,
[
'https://www.googleapis.com/auth/firebase',
'https://www.googleapis.com/auth/firebase.database',
'https://www.googleapis.com/auth/firebase.messaging',
],
null
);
jwtClient.authorize(function (err, tokens) {
if (err) {
reject(err);
return;
}
resolve(tokens.access_token);
});
});
}

我在这里错过了什么? 任何建议将不胜感激

我刚刚意识到我从教程中复制了 fcm 端点的 uri。 我把它改成:

const api = 'https://fcm.googleapis.com/v1/projects/{project-Id}/messages:send';

它奏效了!

最新更新