0
 Error: {"errors": ["Please include a case-sensitive header of Authorization: Basic <YOUR-REST-API-KEY-HERE> with a valid REST API key."], "reference": ["https://documentation.onesignal.com/docs/accounts-and-keys#section-keys-ids"]}

我尝试如下但错误如上

sendNotification = async (data) => {

    const { userId } = await OneSignal.getDeviceState();
    const notificationObj = {
      contents: { en: "Message Body" },
      include_player_ids: [userId],
      Authorization: "Basic APIKEY",
      headings: { en: 'You have new notification' },
      android_channel_id: 'id',
      template_id: 'id',
      buttons: [{ "id": "open_flat", "text": "OPEN HOSTING", "icon": "ic_menu_share" }],
      include_external_user_ids: ["13245-123455"],
    };

    const jsonString = JSON.stringify(notificationObj);

    OneSignal.postNotification(jsonString, (success) => {
      console.log("Success:", success);
    }, (error) => {
      console.log("Error:", error);
    });
      };

    //Sending demo 
    useEffect(() => {
        sendNotification()
      })

我收到错误:错误:{“错误”:[“请包含区分大小写的授权标头:具有有效 REST API 密钥的基本。”],“参考”:[“https://documentation.onesignal. com/docs/accounts-and-keys#section-keys-ids"]}

几个月来,我尝试使用带有内容类型和 Auth 标头的 fetch 发送通知

4

2 回答 2

0

您需要将 APIKEY 替换为您的实际 API 密钥,例如您的 API 密钥是“MY_API_KEY123456”,那么标题应该是 Authorization: “Basic MY_API_KEY123456”

于 2021-06-10T08:30:17.950 回答
0

对于任何人有这个错误

 let headers = {
    'Content-Type': 'application/json; charset=utf-8',
    Authorization: `Basic '<API-KEY-HERE>'`,
};
let endpoint = 'https://onesignal.com/api/v1/notifications';
let params = {
    method: 'POST',
    headers: headers,
    body: JSON.stringify({
        app_id: 'App Id',
        include_external_user_ids: [{'userid'},{'userid2'}], --> Optional
        headings: { en: 'DATA'},
        contents: { en: 'DATA'},
        buttons: [{ "id": 'id', "text": 'OPEN', "icon": "ic_baseline_open_in_new_24" }], --> OPTIONAL
        data: 'Extra data as json'

    }),
};
fetch(endpoint, params).then(res => { console.log('sucess NotiButton') }).catch(error => console.log('error' + error));
于 2021-06-26T12:11:41.683 回答