【问题标题】:HTTP Post Request: Error 400, Firebase Topic MessagingHTTP 发布请求:错误 400,Firebase 主题消息传递
【发布时间】:2017-01-10 02:41:03
【问题描述】:

我正在尝试在 Android 应用程序中实现 Firebase 主题消息传递,并且我正在尝试构建一个 HTTP 发布请求,我收到的响应代码为 400。我查看了各种解决方案,但没有一个似乎有所帮助。

这里是我调用 AsyncTask 子类的地方:

try{new FirebaseSendMessage().execute("Hello world");}
                catch (Exception e) {
                    Log.d("Exception", e.toString());
                }

这是我的异步任务类的子类。

class FirebaseSendMessage  extends AsyncTask<String, Integer, Double> {
private final static String USER_AGENT = "Mozilla/5.0";
private final static String AUTH_KEY = "<My firebase authorization key obtained from firebase>";

private Exception exception;

protected Double doInBackground(String... params) {
    try {
        sendRequest(params);
    } catch (Exception e) {
        this.exception = e;
    }
    return null;
}

protected void onPostExecute(Long l) {
    // TODO: check this.exception
    // TODO: do something with the feed
}


public void sendRequest(String... params) {
    try {
        String urlString = "https://fcm.googleapis.com/fcm/send";
        URL url = new URL(urlString);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setDoOutput(true);
        con.setRequestMethod("POST");
        con.setRequestProperty("Content-Type", "application/json");
        con.setRequestProperty("Authorization", "key=" + AUTH_KEY);
        String postJsonData = "{\"to\": \"/topics/news\"\"data\": {\"message\": \"This is a Firebase Cloud Messaging Topic Message!\"}";
        con.setDoOutput(true);

        DataOutputStream wr = new DataOutputStream(con.getOutputStream());
        wr.writeBytes(postJsonData);
        wr.flush();
        wr.close();

        int responseCode = con.getResponseCode();
        System.out.println("POST Response Code :: " + responseCode);

        if (responseCode == HttpURLConnection.HTTP_OK){
            System.out.println("succeeded");
        }
        /*InputStream is = con.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = null;
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
        //con.disconnect();*/
    }
    catch(IOException e){
        Log.d("exception thrown: ", e.toString());
    }
}

}

错误:I/System.out: POST Response Code :: 400

如果需要额外的代码 sn-ps 来帮助我调试,请告诉我。提前致谢!

【问题讨论】:

  • 请不要在您的 Android 应用程序中包含此代码。您将 AUTH_KEY 暴露给您应用的所有用户,这意味着它可以(并且将会)被黑客找到,然后黑客可以使用它代表您的应用发送消息。
  • 看来firebase要求我这样做。有解决办法吗?
  • 您应该使用应用服务器。或者从 Firebase 控制台发送通知。在客户端 APK 中嵌入服务器密钥不是您应该想要的替代方法。

标签: android http firebase http-post firebase-cloud-messaging


【解决方案1】:

Error 400 表示您的请求中的 JSON 无效:

检查 JSON 消息的格式是否正确并包含有效字段(例如,确保传入正确的数据类型)。

在您的sendRequest 中,您错过了"news\"\"data\" 之间的逗号(,)和右括号(}):

String postJsonData = "{\"to\": \"/topics/news\"\"data\": {\"message\": \"This is a Firebase Cloud Messaging Topic Message!\"}";

看起来像这样:

{"to": "/topics/news/""data":{"message":"...."}

应该是:

String postJsonData = "{\"to\": \"/topics/news\", \"data\": {\"message\": \"This is a Firebase Cloud Messaging Topic Message!\"}}";

以便 JSON 结构正确:

{"to": "/topics/news/",
 "data":{"message":"..."}
}

【讨论】:

  • 这实际上很奇怪 - 我们在生产中使用了几个月的解决方案,现在我们遇到了 400 个错误......仍在调查中
【解决方案2】:

对于那些愿意在您的应用程序中使用身份验证密钥的人。

我建议通过应用程序的 SHA-1 手动加密密钥,并在运行时使用 SHA-1 代码对其进行解密。

【讨论】:

  • 这使得获取密钥的时间延长了几分钟。除非您希望人们弄乱您的应用程序,否则您首先不应该在客户端应用程序上使用它。用户服务器或其他 Firebase 访问方法。
猜你喜欢
  • 2020-11-29
  • 1970-01-01
  • 2013-07-21
  • 2018-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-19
  • 1970-01-01
相关资源
最近更新 更多