从服务器应用程序连接到FCM时获取“服务器返回的HTTP响应代码:400”

时间:2019-06-05 22:13:24

标签: 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;
    }

1 个答案:

答案 0 :(得分:0)

URL看起来更像https://fcm.googleapis.com/fcm/send

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

确保将服务器密钥包含在Authorization标头中(看来您还可以)。