Search code examples
restoauth-2.0push-notificationfirebase-cloud-messagingpostman

Get authorization token from OAUTH2 for send push notifications to Firebase with REST Calls


Im trying to get Authorization code for send push notifications throug new Firebase cloud messaging API. For this pourpose i need to get OAuth2.0 token. The problem its im only do a REST calls and im trying to do this from POSTMAN.

I making post to this URL: https://accounts.google.com/o/oauth2/auth?client_id=xxxxxx&response_type=token&redirect_uri=xxxxxxxx&scope=https://www.googleapis.com/auth/cloud-platform&include_granted_scopes=true

But google return cant load the page without javascript permissions (In postman). When I put this URL in navigator open a login page from google... I want to make a REST call and have the message from that call return the authorization code and then request the temporary token, not send me to a Google login page. What can I do?

I cant use Ouath client from java or something similar, only REST calls.

Thanks you for your time.

How can I make the call to receive the authorization code for after requesting the temporary token to perform Push notifications through Firebase?


Solution

  • If you're using the FCM HTTP v1 API to send messages, you can authorize the requests using a short-lived OAuth 2.0 access token. To generate this token, you can use Google Application Default Credentials (in Google server environments) or manually obtain the credentials from a JSON private key file generated for a service account.

    For the sake of testing, below are the options you can try to generate an OAuth 2.0 access token:

    A) Using Google Developers OAuth 2.0 Playground

    Select and Authorize Firebase Cloud Messaging API v1

    1. Select https://www.googleapis.com/auth/firebase.messaging as the scope
    2. Click Authorize APIs
    3. Once Authorization code is generated, exchange it to access token
    4. Copy the access token and use it in the request

    B) Using Firebase Cloud Messaging Node.js Quickstart Project

    1. Install Firebase Admin SDK and Node.js
    2. Download the quickstart project and follow the guide in README.md file
    3. Update index.js and log the access token
    4. Copy the access token and use it in the request

    Note that the usual prefix of access token is ya29.

    Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA

    See this documentation on how to authorize send request from your app server.