2
votes

Am trying to test my web push notification from postman

my app id is thepostman-2018 so I am sending post requests to the url https://fcm.googleapis.com/v1/projects/thepostman-2018/messages:send event though I have set Authentication header and passed my Server key

i am getting this response

{
    "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
Try giving token name start with 'b'. Like bearer *********. Might help you. - Veeresham Tammali
Is there a reason why you're using the REST API over the provided SDKs? - Cisco
Did you solve this? I had the key but can't get this working. - Allan Macatingrao

1 Answers

5
votes

FCM v1 requests do not use the API key from the Firebase console to authorize requests. Instead, they use credentials retrieved by authenticating using the Service Account Key downloaded from the Firebase console. For example, this is how you generate the token using 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);
    });
  });
}

See the guide for more details.