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

标签 java firebase firebase-cloud-messaging

我正在尝试从我的服务器应用程序访问 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/

相关文章:

java.lang.NullPointerException : Attempt to invoke virtual method . .. 在空对象引用上

java - 并发 - foreach 循环中的非阻塞设计

node.js - 如何在没有网络的情况下安装 Node.js 包?

android - 何时正确订阅 firebase 中的主题

java - swing:关于调整 JFrame 大小和条件滚动

java - 查询 KafkaAvroDeserializer 使用的默认 SchemaRegistryClient

javascript - 使用 React Native 的 Firebase 查询未显示在我的屏幕上(但显示在 console.log 上)

javascript - 从 HTTP 函数将图像文件写入 Firebase Storage

来自 OAuth2 playground 的 Firebase 不记名 token

ios - 如何为 Firebase 添加通知服务扩展?