Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What Bearer token should I be using for Firebase Cloud Messaging testing?

I am trying to send a test notification using Firebase Cloud Messaging via Postman. I'm doing a POST to this url

https://fcm.googleapis.com/v1/projects/[my project name]/messages:send 

The Authorization tab in Postman is set to No Auth and my Headers tab looks like this

Content-Type: application/json Authorization: Bearer [server key] 

[server key] is a newly generated server key in the 'Cloud Messaging' tab of my Firebase project's 'Settings' area. I keep getting this error in 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" } 

Based on everything I can find, I'm using the right token, but it seems Google disagrees. What should I be sending as the Authorization header to get past this error?

like image 814
sonicblis Avatar asked May 17 '18 19:05

sonicblis


People also ask

What is Bearer Token in Firebase?

The Bearer Token is the result of getting an OAuth access token with your firebase service account. Get yourself a Firebase service account key. Go to your firebase console > Settings > Service Accounts. If your on Firebase Admin SDK generate new private key.

What is token in firebase cloud messaging?

On initial startup of your app, the FCM SDK generates a registration token for the client app instance. If you want to target single devices or create device groups, you'll need to access this token. You can access the token's value by extending FirebaseInstanceIdService.


1 Answers

Steps to get Authentication Bearer:

  1. Got to Google OAuth Playground: https://developers.google.com/oauthplayground
  2. In the "Input your own scopes" for FCM use this url: https://www.googleapis.com/auth/firebase.messaging
  3. Tap Authorize API.
  4. Pick correct user for authorisation and allow access.
  5. In the Step 2: Exchange authorization code for tokens tap Exchange authorisation code for tokens.
  6. Access token is your Bearer.

Steps to send FCM throw Postman:

  1. URL to send: https://fcm.googleapis.com/v1/projects/projectid-34543/messages:send
  2. Request Type: POST
  3. Headers: Content-Type -> application/json & Authorization -> Bearer
  4. In the body section enter APS payload with the right device token.
  5. Click send.

enter image description here

In case you want to use cURL, for a data-notification:

curl --location --request POST 'https://fcm.googleapis.com/v1/projects/your-project-id/messages:send' \ --header 'Content-Type: application/json' \ --header 'Authorization: Bearer your-access-token-*****-wqewe' \ --data-raw '{     "message": {         "token": "device-token-qwfqwee-***-qefwe",         "data": {             "Key1": "val1",             "Key2": "val2"         }     } }' 
like image 101
Ramis Avatar answered Sep 21 '22 01:09

Ramis