ToothlessGear / node-gcm

A NodeJS wrapper library port to send data to Android devices via Google Cloud Messaging
https://github.com/ToothlessGear/node-gcm
Other
1.3k stars 208 forks source link

Support for Google Service Accounts authorization #367

Closed guss77 closed 1 year ago

guss77 commented 1 year ago

I may be missing some boat here, but as far as I can tell, new Firebase projects cannot generate a server key - see screenshot - and you must use service accounts.

Screenshot_20220908_110910

As far as I can tell, node-gcm does not support service accounts - I must provide an "API Key" which is no longer a thing in current Firebase projects.

Is it possible to support service accounts for authorization?

eladnava commented 1 year ago

Hi @guss77, Thanks for creating this issue.

node-gcm currently only supports legacy Cloud Messaging API server keys for authentication. I've updated the instructions on how to obtain this legacy server key in https://github.com/ToothlessGear/node-gcm/commit/d5cfe6a00aba6313349838b694016461220d80a8, they are as follows:

You need to generate a Legacy API Server Key (Click the gear next to FCM project name) > Project Settings > Cloud Messaging -> Cloud Messaging API (Legacy) -> Click the 3 dots -> Manage API in Google Cloud Console -> Enable -> Go back and refresh Firebase Cloud Messaging project settings page to view your Server key.

Please let me know if you face any difficulties.

guss77 commented 1 year ago

Thanks @eladnava.

I'm generally loath to create a new project that relies on the disabled-by-default "legacy" API, because it is only a matter of time until it is completely removed, so I eventually used the firebase-admin official library (instead of using the node-pushnotifications abstraction library that uses node-gcm), because it works with a service account - only to find out that it doesn't work unless you enable the "legacy" API (something to do with permissions, you don't actually need the legacy server key for the official library). so... 🤷

But still, I think you should update the library to use service accounts and the new v1 API to keep it relevant for the future. BTW - I also looked at fcm-node as an alternative and it is an abstraction over the legacy + v1 API, and it implements the v1 API by calling into firebase-admin, and as I have no intention of using the legacy API ever, I might as well just use firebase-admin directly.

I also think that the notice fcm-node has at the top of their README is important and I think that they are right in that as they don't plan to offer an alternative to firebase-admin (its just a thin wrapper on top of it and is only useful if you plan to migrate a large codebase from "legacy" to "v1"), they might as well call themselves "deprecated" and point people at the official library.

I do think that the official library is overkill if you just want to send Firebase messages to devices, you should be able to use a light implementation that does only that, using a non-deprecated API, and I wish node-gcm was it (especially as it is used by the much needed larger abstraction layer of node-pushnotifications).