导入现有的推送订阅

时间:2019-03-18 18:44:13

标签: node.js firebase-cloud-messaging onesignal google-auth-library

我已经有Onesignal服务的推送订阅,并且尝试为Firebase Cloud Messaging重新订阅它。 使用example时出现错误:

{
    error: {
        code: 401,
        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.',
        status: 'UNAUTHENTICATED'
   }
}

我的请求示例:

let https = require('https');
let google = require('google-auth-library');
let key = require(process.cwd() + "/credentials/creadential.json");
let jwt = new google.JWT(
    key.client_email,
    null,
    key.private_key,
    ['https://www.googleapis.com/auth/firebase.messaging'],
    null
);
let authTokens = await jwt.authorize();
let cryptoKey = "SERVER_PUBLIC_KEY";
let headers = {
    "Content-Type": "application/json",
    "Authorization": "Bearer " + authTokens.access_token,
    "Crypto-Key": "p256ecdsa=" + cryptoKey,
};  
let options = {
    method: "POST",
    headers: headers
};
let message = {
    "endpoint": "https://fcm.googleapis.com/fcm/send/....",
    "keys": {
        "auth": "...",
        "p256dh": "..."
    }
};
let jsonMessage = JSON.stringify(message);
let req = https.request("https://iid.googleapis.com/v1/web/iid", options, function(res) {
    res.on('data', (data) => {
        console.log("Response:");
        console.log(JSON.parse(data));
    });
});

req.on('error', function(e) {
    console.log("ERROR:");
    console.log(e);
});

req.write(jsonMessage);
req.end();

在互联网上,我找不到解决方案。

0 个答案:

没有答案