Firebase 401 unauthorized error FCM

2019-01-31 23:41发布

I'm trying to test out Firebase Cloud messaging APIs as all functionality is not available from console (notably customization of notifications when app is in background). But for some reasons, I cannot get it to work, and it always shows up 401 error. I investigated the reasons for this, and tried it after regenerating new server key, but the error remains constant. Surprisingly, when I generated a new server key, it is not reflected in Firebase console and it shows server key as empty. Also, I tried adding my IP address to the server whitelist IPs but still no luck. I've attached a screenshot of a request that I did with Postman (I substitue the server key in place of serverKey.

I'm stuck on this for a few hours and would really appreciate some help.enter image description here

7条回答
Anthone
2楼-- · 2019-01-31 23:57

I too am facing the Same problem... I am using curl in php for posting and it works only if I have php files stored on my LocalHost server. When I try to use access the files via a free hosting online, then it say Unautorized 401.

So I would suggest if you can, use the Localhost.

查看更多
仙女界的扛把子
3楼-- · 2019-01-31 23:59

I don't know if someone uses the [Web API Key] as the [YOUR_SERVER_KEY] for POSTMAN test and keep getting '401 Error'. [Web API Key] is not [YOUR_SERVER_KEY].

You should go to your Firebase console and check this:

to get the correct Server key.

Hope it help.

查看更多
Evening l夕情丶
4楼-- · 2019-02-01 00:08

I was facing the same problem, i solved it by using the following steps

1- In the server from where you are sending push, Use the browser key only, you can get it from Firebase console or google api console as I have highlighted in the below images:-

Google api console

Firebase console, click on the project-->settings

Note : The Firebase console web api key and google console browser key are the same you can use either of them

enter image description here

enter image description here

2- If you follow the first step only you will get the Unauthorized error, to resolve this you need to authorize your browser key in google console by adding your server IP address from where you will send the push. Click on the edit pencil icon on the right side of your browser key in google api console, above first image

enter image description here

After adding your Ip address click save Make sure that your device token is not empty on which you are sending the push, I hope your push will be sent successfully now.

查看更多
何必那么认真
5楼-- · 2019-02-01 00:15

I noticed from your screenshot that you were using "key: serverKey". Could you try using "key=serverKey" instead?

Also you don't need to have "POST fcm.googleapus.com/fcm/send"; this is not proper json and would explain the error you are seeing. The URL of the request is already defined elsewhere so remove it from the payload.

查看更多
Summer. ? 凉城
7楼-- · 2019-02-01 00:19

I have the same problem at Server Side Code(C#).

You basically used wrong Server Key (or API Key) for service side code.

Follow below Link over stackoverflow posted by me (Helpful to find Server Key (or API Key) )

FCM (Firebase Cloud Messaging) Push Notification with Asp.Net

查看更多
登录 后发表回答