firebase(fcm)表示401未经授权 [英] firebase (fcm) says 401 unauthorized
本文介绍了firebase(fcm)表示401未经授权的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
private void sendMsg(){
DBManager dbManager = DBManager.getInstance();
ArrayList< String> firebaseIds;
尝试{
ResultSet rs = dbManager.getRegisteredFirebaseDevice(); (rs.next()){
System.out.println(rs.getString(1));
firebaseIds.add(rs.getString(1));
}
} catch(SQLException e){
e.printStackTrace();
}
字符串url =https://fcm.googleapis.com/fcm/send;
URL obj =新的URL(url);
HttpURLConnection con =(HttpURLConnection)obj.openConnection();
//添加reuqest头文件
con.setRequestMethod(POST);
con.setRequestProperty(Authorization:key,AIzaSyAl6S936qt_NKKFwwbd-NEmiSGIL7G_yJc);
con.setRequestProperty(Content-Type,application / json);
// String msg =添加了新的设计+ getCategory(designCategory)+。Design no。+ designNo;
// String urlParameters =data.msg =+ msg +& registration_id =+ firebaseIds.get(0);
JSONObject msg = new JSONObject();
msg.put(msg,添加新设计+ getCategory(designCategory)+。Design no。+ designNo);
JSONObject parent = new JSONObject();
parent.put(to,firebaseIds.get(0));
parent.put(data,msg);
//字符串urlParameters =registration_id =+ firebaseIds.get(0);
//发送帖子请求
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(\\\
发送'POST'请求到URL:+ url);
System.out.println(Post parameters:+ parent.toString());
System.out.println(Response Code:+ responseCode ++ con.getResponseMessage());
$ b当我调用上面的代码的时候, 401未授权
。我无法理解为什么我得到这个错误。我使用了正确的服务器密钥。在我使用的策略中是否有语法错误或错误?
我遵循 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>);
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());
}
When I call the above code, it gives me response as 401 Unauthorized
. I am not able to understand why I am getting this error. I have used proper server key. Is there any syntax error or anything wrong in the strategy used by me.
I have followed the https://firebase.google.com/docs/cloud-messaging/server#implementing-http-connection-server-protocol documentation
解决方案 Try replacing:
con.setRequestProperty("Authorization: key", "<YOUR API KEY>");
with:
con.setRequestProperty("Authorization", "key=<YOUR API KEY>");
这篇关于firebase(fcm)表示401未经授权的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文