Ad

Firebase Bearer Token From OAuth2 Playground

I'm trying to test my application that uses Firebase for push notifications using postman.

I'm specifically testing the Http v1 Api, and looking how to authorize the request.

What I need to get right is getting the OAuth2 token to use in Postman, which I should be able to do on the OAuth 2.0 playground although I'm not sure how.

I have my privatkey.json file that I've downloaded from the firebase console, I just need to know how to use it to get the token that I would add as a bearer authorization header for my POST requests

Ad

Answer

I was able to send a message through the FCM v1 HTTP API by requesting the following scopes in the OAuth2 playground:

email, https://www.googleapis.com/auth/firebase.messaging

Specifying scopes in OAuth2 platground

After authorizing this, I exchanged the authorization code for refresh and access tokens.

Exchanging authorization code for tokens

I then passed the resulting access token into the call with FCM:

curl -X POST -H "Authorization: Bearer MY_ACCESS_TOKEN" -H "Content-Type: application/json" -d '{"message":{
  "notification": {
    "title": "FCM Message",
    "body": "This is an FCM Message",
  },
  "token": "MY_DEVICE_TOKEN"
  }
}' https://fcm.googleapis.com/v1/projects/MY_PROJECT_ID/messages:send

In the above CURL request replace the following placeholders with the relevant values for you:


The FCM documentation on authenticating FCM v1 requests may be confusing since it only calls out the OAuth2 token. It actually first generates a self-signed JWT (JSON Web Token) by calling new google.auth.JWT(...). This involves downloading a private key, and generating the JWT locally through a JWT library.

The self-signed JWT is then passed to jwtClient.authorize(...), which gives back tokens including an access_token. The latter is an OAuth2 access token, similar to the one we got above.

Ad
source: stackoverflow.com
Ad