firebase (fcm) 说 401 未经授权
firebase (fcm) says 401 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+"®istration_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());
}
当我调用上面的代码时,它给我的响应是 401 Unauthorized
。我无法理解为什么会出现此错误。我使用了正确的服务器密钥。我使用的策略是否有任何语法错误或任何错误。
我已遵循 https://firebase.google.com/docs/cloud-messaging/server#implementing-http-connection-server-protocol 文档
尝试替换:
con.setRequestProperty("Authorization: key", "<YOUR API KEY>");
与:
con.setRequestProperty("Authorization", "key=<YOUR API KEY>");
刚刚为我解决了这个问题,我更改了服务器 API 密钥,它在 FCM 控制台的云消息传递选项卡上给出。在Project Overview,Manage,有Cloud Messaging选项卡,它显示了一个SERVER API KEY use that may be。 json文件中的client_api键和SERVER_API_KEY不同!!
https://firebase.google.com/docs/cloud-messaging/server
"Make sure this is the server key, whose value is available in the Cloud Messaging tab of the Firebase console Settings pane. Android, iOS, and browser keys are rejected by FCM."
根据 Harco 的回答,在 Firebase 控制台中查看 "Project settings"(单击齿轮图标)并选择 "Cloud Messaging" 选项卡。 "Server key" 就是你需要的。
实际日期为 2017.06.28.
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+"®istration_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());
}
当我调用上面的代码时,它给我的响应是 401 Unauthorized
。我无法理解为什么会出现此错误。我使用了正确的服务器密钥。我使用的策略是否有任何语法错误或任何错误。
我已遵循 https://firebase.google.com/docs/cloud-messaging/server#implementing-http-connection-server-protocol 文档
尝试替换:
con.setRequestProperty("Authorization: key", "<YOUR API KEY>");
与:
con.setRequestProperty("Authorization", "key=<YOUR API KEY>");
刚刚为我解决了这个问题,我更改了服务器 API 密钥,它在 FCM 控制台的云消息传递选项卡上给出。在Project Overview,Manage,有Cloud Messaging选项卡,它显示了一个SERVER API KEY use that may be。 json文件中的client_api键和SERVER_API_KEY不同!!
https://firebase.google.com/docs/cloud-messaging/server
"Make sure this is the server key, whose value is available in the Cloud Messaging tab of the Firebase console Settings pane. Android, iOS, and browser keys are rejected by FCM."
根据 Harco 的回答,在 Firebase 控制台中查看 "Project settings"(单击齿轮图标)并选择 "Cloud Messaging" 选项卡。 "Server key" 就是你需要的。
实际日期为 2017.06.28.