Lawrence Colombo
Lawrence Colombo

Reputation: 365

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

Upvotes: 2

Views: 3373

Answers (2)

Lawrence Colombo
Lawrence Colombo

Reputation: 365

I created a small project on hithub that includes both a postman collection and environment and nodejs project that uses the downloaded service-key.json to generate an access token which solves my problem above. It's not as elagent as using only postman (which to me seems impossible), but it works well enough since the access tokens live for about an hour.

Upvotes: 1

Frank van Puffelen
Frank van Puffelen

Reputation: 599631

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.

Upvotes: 12

Related Questions