Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Firebase/GCM push is returning 401 error even though a curl request works as expected #346

Closed
petergzli opened this issue Mar 21, 2017 · 7 comments

Comments

@petergzli
Copy link

I believe I set up everything correctly, and trying to test with a simple one device registration ID push. However, I keep getting the 401 error even though I know for a fact the keys work, i.e. I do a regular curl on http://fcm.googleapis.com/fcm/send using the SAME server_key I set up for the Rpush::Gcm::App as well as the same registration ID I intended to send the message to. Because the curl works perfectly, I don't think it's actually an auth_key issue even though Rpush keeps telling me Unauthorized, check your App auth_key.

Any ideas what I could have done to set it up incorrectly?

I set up the new app like so...
app = Rpush::Gcm::App.new
app.name = "android_app"
app.auth_key = "..."
app.connections = 1
app.save!

And tested a push like so...

n = Rpush::Gcm::Notification.new
n.app = Rpush::Gcm::App.find_by_name("android_app")
n.registration_ids = ["..."]
n.data = { message: "hi mom!" }
n.save!

Rpush.push returns the 401...

@aried3r
Copy link
Member

aried3r commented Mar 21, 2017 via email

@petergzli
Copy link
Author

I'm using rPush, 2.7.0 @aried3r the latest one.

@aried3r
Copy link
Member

aried3r commented Mar 23, 2017

I'm using Rpush 2.7.0 as well on a production server with FCM and it works without problems.

My only guess is that it's a configuration error, since it seems to connect to the FCM server just fine, but with a 401 HTTP code.

Can you please double check your configuration and check your logs as well for the full error message?

@petergzli
Copy link
Author

Thanks for replying @aried3r

The logs return the same error, Rpush::DeliveryError, Unable to deliver notification 8, received error 401 (Unauthorized, check your App auth_key.)

The strange thing is when I do a curl request, it works 100%. So I don't think my keys/registration-id's are off. But maybe I'm setting the variables incorrectly?

The main server key is the Rpush::Gcm::App auth_key, correct? That's what I set for it.

And obviously when sending the actual push, the registration_id is the id matched with the phone for Rpush::Gcm::Notification registration.ids

data is just a simple message. So I don't know where the auth error is breaking since the CURL request works?

@aried3r
Copy link
Member

aried3r commented Mar 25, 2017

I'm using Postman to test this, although it works just fine using Rpush.

I set two headers, according to the docs which Rpush does as well.

The value of the Authorization header was taken from (in your case) Rpush::Gcm::App.find_by(name: 'android_app').auth_key. Can you please verify that this is the exact same key you used in your curl command?
And double check with notification.app.auth_key where notification is your Rpush::Client::ActiveRecord::Gcm::Notification. It should not differ since you manually set the Rpush::Gcm::App, but usually the code that initializes the app is in a different place than where notifications are being build. So please do check that it's the same, has no spaces, is not missing any characters etc.
If you're unsure, check programmatically if the two strings (from the curl command) and the one in Rpush match exactly.

I guess somewhere in your code where you build notifications you have something similar to the following, heavily simplified code:

app = Rpush::Gcm::App.find_by(name: 'android_app')
puts "App auth_key: #{app.auth_key}"

or where you already have your notification:

puts notification.app.auth_key

The payload I used in Postman is very simple, please replace the registration ID with a valid one:

{
  "registration_ids": ["invalid"],
  "data": {
    "foobar": "bazbar"
  },
  "dry-run": true
}

I cannot really help you any further with debugging, since it does not seem to be Rpush's fault.

Check if you are using the newer, shorter Server Keys. I guess you already know, but this SO post also explains how to get there in the FCM Console. However, we are still using the old "legacy" server keys in a project and that also works for us (as they are still supported).

@aried3r
Copy link
Member

aried3r commented Feb 22, 2018

Closing since there was no more activity. I'll reopen if necessary.

@schmittsfn
Copy link

Here a sample curl to save time for any dev researching this and wanting to test:

curl --location --request POST 'https://fcm.googleapis.com/fcm/send' \
--header 'Authorization: key=YOURSERVERKEY' \
--header 'Content-Type: application/json' \
--data-raw '{
    "registration_ids": [
        "YOURDEVICEID"
    ],
    "data": {
        "foobar": "bazbar"
    },
    "dry-run": true
}'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants