Firebase (fcm) says 401 is unauthorized

private void sendMsg() { DBManager dbManager = DBManager.getInstance(); ArrayList<String> firebaseIds; try { ResultSet rs= dbManager.getRegisteredFirebaseDevice(); while(rs.next()){ System.out.println(rs.getString(1)); firebaseIds.add(rs.getString(1)); } } catch (SQLException e) { e.printStackTrace(); } String url = "https://fcm.googleapis.com/fcm/send"; URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); // add reuqest header con.setRequestMethod("POST"); con.setRequestProperty("Authorization: key", "AIzaSyAl6S936qt_NKKFwwbd-NEmiSGIL7G_yJc"); con.setRequestProperty("Content-Type", "application/json"); // String msg="New design added in "+getCategory(designCategory)+". Design no."+designNo; // String urlParameters = "data.msg="+msg+"&registration_id="+firebaseIds.get(0); JSONObject msg=new JSONObject(); msg.put("msg","New design added in "+getCategory(designCategory)+". Design no."+designNo); JSONObject parent=new JSONObject(); parent.put("to", firebaseIds.get(0)); parent.put("data", msg); // String urlParameters = "registration_id="+firebaseIds.get(0); // Send post request con.setDoOutput(true); OutputStreamWriter wr= new OutputStreamWriter(con.getOutputStream()); wr.write(parent.toString()); // DataOutputStream wr = new DataOutputStream(con.getOutputStream()); // wr.writeBytes(urlParameters); // wr.flush(); // wr.close(); int responseCode = con.getResponseCode(); System.out.println("\nSending 'POST' request to URL : " + url); System.out.println("Post parameters : " + parent.toString()); System.out.println("Response Code : " + responseCode+" "+con.getResponseMessage()); } 

When I call the above code, it gives me the answer as 401 Unauthorized . I cannot understand why I am getting this error. I used the correct server key. Is there a syntax error or something is wrong with the strategy used by me.

I have completed the https://firebase.google.com/docs/cloud-messaging/server#implementing-http-connection-server-protocol documentation

+6
source share
4 answers

Try replacing:

 con.setRequestProperty("Authorization: key", "<YOUR API KEY>"); 

from:

 con.setRequestProperty("Authorization", "key=<YOUR API KEY>"); 
+8
source

Just solved the problem for me, I changed the server API key, which is listed on the Cloud Messages tab in the FCM console. In the "Project Overview" "Management" section there is a "Cloud Messages" tab, which shows the use of the "SERVER KEY" key, which can be. In the json file, the client_api key and SERVER_API_KEY are different!

+9
source

Based on Harco's answer in the Firebase console, browse to Project Settings (click the gear icon) and select the Cloud Messaging tab. The "server key" is what you need.

enter image description here

In fact, on 2017.06.28.

+3
source

https://firebase.google.com/docs/cloud-messaging/server

"Make sure that this is a server key whose value is available on the Cloud Messages tab of the Firebase Panel Settings panel. Android, iOS and browser keys are rejected by FCM."

-one
source

All Articles