gpt4 book ai didi

java - 从服务器应用程序连接到 FCM 时获取 "Server returned HTTP response code: 400"

转载 作者:行者123 更新时间:2023-12-02 04:47:49 25 4
gpt4 key购买 nike

我正在尝试从我的服务器应用程序访问 FCM。检查 here 中提供的示例!但出现错误:

java.io.IOException: Server returned HTTP response code: 400 for URL: https://fcm.googleapis.com/v1/projects/unsaarmdm/messages:send
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1627)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
at asd.MessagesClientFCMServer.sendMessageToFcm(MessagesClientFCMServer.java:66)
at asd.MessagesClientFCMServer.sendData(MessagesClientFCMServer.java:40)
at asd.MessagesClientFCMServer.main(MessagesClientFCMServer.java:37)

我已经创建了名为“unsaarmdm”的 Firebase 项目并下载了包含私钥的 json 文件。还将 Google API 客户端库添加到我的项目中。

下面是代码片段:

    private static String FCM_DEALS_ENDPOINT 
= "https://fcm.googleapis.com/v1/projects/unsaarmdm/messages:send";
//https://fcm.googleapis.com/v1/projects/unsaarmdm/messages:send

public static void main(String args[]) {
MessagesClientFCMServer fcmClient = new MessagesClientFCMServer();
fcmClient.sendData();
}
private void sendData(){
sendMessageToFcm(getFcmMessageJSONData());
}


//Using HttpURLConnection it send http post request containing data to FCM server
private void sendMessageToFcm(String postData) {
try {

HttpURLConnection httpConn = getConnection();

DataOutputStream wr = new DataOutputStream(httpConn.getOutputStream());
wr.writeBytes(postData);
wr.flush();
wr.close();

BufferedReader in = new BufferedReader(
new InputStreamReader(httpConn.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();

while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();

log.info(response.toString());

} catch (Exception e) {
e.printStackTrace();
}
}

private static String getAccessToken() throws IOException {
GoogleCredential googleCredential = GoogleCredential
.fromStream(new FileInputStream("C:/dev/Firebase_private_key/unsaarmdm-firebase-adminsdk.json"))
.createScoped(Arrays.asList(SCOPE));
googleCredential.refreshToken();
String token = googleCredential.getAccessToken();
return token;
}

//create HttpURLConnection setting Authorization token
//and Content-Type header
private HttpURLConnection getConnection() throws Exception {
URL url = new URL(FCM_DEALS_ENDPOINT);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestProperty("Authorization", "Bearer " + getAccessToken());
httpURLConnection.setRequestProperty("Content-Type", "application/json; UTF-8");
httpURLConnection.setDoOutput(true);
httpURLConnection.setUseCaches(false);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.connect();
return httpURLConnection;
}

最佳答案

该网址看起来更像https://fcm.googleapis.com/fcm/send

这是一个奇怪的部分,我知道文档说要使用您正在使用的 URL,但我有一个工作实现(并且我知道我花了一些时间对此进行调试),使用我刚刚提到的 URL,尝试并让我知道:)我们都可以学习。

确保将服务器 key 包含在 Authorization header 中(看起来您做得不错)。

关于java - 从服务器应用程序连接到 FCM 时获取 "Server returned HTTP response code: 400",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56468732/

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