FCM MissingAuthorization问题

时间:2018-04-06 11:04:42

标签: javascript jquery firebase firebase-cloud-messaging

这是我的代码:

function sendToken(token) {
  $.ajax({
    url: 'https://iid.googleapis.com/iid/v1/' + token + '/rel/topics/IoT',
    method: 'POST',
    headers: {
      Authorization: 'key=' + server_key
    },
    contentType: 'application/json',
    cache: false,
    success: function(response) {
      console.log(response);
    },
    error: function(response) {
      console.log(response);
    }
  });
}

回复是{"error":"MissingAuthorization"}

我被困住了,无法理解为什么这不起作用。

1 个答案:

答案 0 :(得分:0)

我也遇到了同样的问题,所以我使用了 fetch 并且它有效。

fetch('https://iid.googleapis.com/iid/v1/'+token+'/rel/topics/'+topic, {
method: 'POST',
headers: new Headers({
  'Authorization': 'key='+server_key
})
}).then(response => {
    if (response.status < 200 || response.status >= 400) {
      throw 'Error subscribing to topic: '+response.status + ' - ' + response.text();
    }
    console.log('Subscribed to "'+topic+'"');
  }).catch(error => {
    console.error(error);
  })