gpt4 book ai didi

android - firebase (fcm) 说 401 未经授权

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:22:38 25 4
gpt4 key购买 nike

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());

}

当我调用上面的代码时,它给我的响应是 401 Unauthorized。我无法理解为什么会出现此错误。我使用了正确的服务器 key 。我使用的策略是否有任何语法错误或任何错误。

我已经关注了 https://firebase.google.com/docs/cloud-messaging/server#implementing-http-connection-server-protocol文档

最佳答案

刚刚为我解决了这个问题,我更改了服务器 API key ,它在 FCM 控制台的云消息传递选项卡上给出。在Project Overview, Manage, 有Cloud Messaging 选项卡,它显示了一个SERVER API KEY 可能被使用。 json文件中的client_api key和SERVER_API_KEY不同!!

关于android - firebase (fcm) 说 401 未经授权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37963356/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com