正在获取请求具有无效的身份验证凭据错误FCM

时间:2018-06-27 13:38:11

标签: firebase firebase-cloud-messaging postman

我正在尝试测试邮递员的网络推送通知

我的应用程序ID为 thepostman-2018 ,因此我正在向url发送发布请求 https://fcm.googleapis.com/v1/projects/thepostman-2018/messages:send 事件,尽管我设置了Authentication标头并传递了我的服务器密钥

我收到了这个回复

{
    "error": {
        "code": 401,
        "message": "Request had invalid authentication credentials. 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"
    }
}

enter image description here

1 个答案:

答案 0 :(得分:4)

FCM v1请求不使用Firebase控制台中的API密钥来授权请求​​。取而代之的是,他们使用通过从Firebase控制台下载的服务帐户密钥进行身份验证来检索的凭据。例如,这是使用Node.js生成令牌的方式:

 function getAccessToken() {
  return new Promise(function(resolve, reject) {
    var key = require('./service-account.json');
    var jwtClient = new google.auth.JWT(
      key.client_email,
      null,
      key.private_key,
      SCOPES,
      null
    );
    jwtClient.authorize(function(err, tokens) {
      if (err) {
        reject(err);
        return;
      }
      resolve(tokens.access_token);
    });
  });
}

有关更多详细信息,请参见guide