Solved Discord WebHooks

Discussion in 'Spigot Plugin Development' started by Bobcatsss, Mar 5, 2020.

  1. I'm trying to use Discord WebHooks however every time i try to send a message i got a 400 response, before this i had a 403 connection refused but adding a "User-Agent" seemed to change this to 400.

    First time trying this and I've been on google for the past 2 hours, any help would be appricated.

    Code used to send the message:
    Code (Text):
       
    public void sendMessage(String message, String url) throws IOException {
            URL u = new URL(url);
            JSONObject payload = new JSONObject();
            payload.put("username", "Pancake");
            payload.put("content", message);
            Gson gson = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();
            String json = gson.toJson(payload);
            HttpsURLConnection connection = (HttpsURLConnection) u.openConnection();
            connection.addRequestProperty("Content-Type", "application/json");
            connection.addRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36");
            connection.setDoOutput(true);
            connection.setRequestMethod("POST");

            OutputStream stream = connection.getOutputStream();
            stream.write(json.toString().getBytes());
            stream.flush();
            stream.close();
        }
    Code (Text):
    05.03 18:49:35 [Server] Async Chat Thread - #0/WARN java.io.IOException: Server returned HTTP response code: 400 for URL: <my webhook url>
    05.03 18:49:35 [Server] Async Chat Thread - #0/WARN at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1894)
    05.03 18:49:35 [Server] Async Chat Thread - #0/WARN at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1492)
    05.03 18:49:35 [Server] Async Chat Thread - #0/WARN at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:263)
    05.03 18:49:35 [Server] Async Chat Thread - #0/WARN at me.bobcatsss.staffutils.listeners.ChatListener.test(ChatListener.java:55)
    05.03 18:49:35 [Server] Async Chat Thread - #0/WARN at me.bobcatsss.staffutils.listeners.ChatListener.onChat(ChatListener.java:96)
    05.03 18:49:35 [Server] Async Chat Thread - #0/WARN at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    05.03 18:49:35 [Server] Async Chat Thread - #0/WARN at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    05.03 18:49:35 [Server] Async Chat Thread - #0/WARN at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    05.03 18:49:35 [Server] Async Chat Thread - #0/WARN at java.lang.reflect.Method.invoke(Method.java:498)
    05.03 18:49:35 [Server] Async Chat Thread - #0/WARN at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:315)
    05.03 18:49:35 [Server] Async Chat Thread - #0/WARN at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:70)
    05.03 18:49:35 [Server] Async Chat Thread - #0/WARN at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:541)
    05.03 18:49:35 [Server] Async Chat Thread - #0/WARN at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:528)
    05.03 18:49:35 [Server] Async Chat Thread - #0/WARN at net.minecraft.server.v1_15_R1.PlayerConnection.chat(PlayerConnection.java:1580)
    05.03 18:49:35 [Server] Async Chat Thread - #0/WARN at net.minecraft.server.v1_15_R1.PlayerConnection.a(PlayerConnection.java:1518)
    05.03 18:49:35 [Server] Async Chat Thread - #0/WARN at net.minecraft.server.v1_15_R1.PacketPlayInChat$1.run(PacketPlayInChat.java:41)
    05.03 18:49:35 [Server] Async Chat Thread - #0/WARN at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    05.03 18:49:35 [Server] Async Chat Thread - #0/WARN at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    05.03 18:49:35 [Server] Async Chat Thread - #0/WARN at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    05.03 18:49:35 [Server] Async Chat Thread - #0/WARN at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    05.03 18:49:35 [Server] Async Chat Thread - #0/WARN at java.lang.Thread.run(Thread.java:748)
     
  2. Well it seems with no User-Agent request property, the server was giving you a 403 Forbidden error, which means the token you supplied to authenticate with Discord is invalid.
    It is not in this code snippet, so I don't think you are supplying one, if you are and need to show it, redact is so your PRIVATE token is not visible to the public.

    I think the string you are using for your user agent is invalid, I can see some characters Discord may consider illegal for example '/' and ';'.
    The user agent should be a reference to the Agent (the program) that is accessing Discord on the User's behalf.
    I would recommend changing it to something that actually reflects this like:
    Code (Java):
    connection.addRequestProperty("User-Agent","Your Plugin Name 1.0");
    That should stop the the 400 error, but I still expect there to be a 403.
    For that you need to attach your token in the authorization header.
    You can read up on how to do that here:
    https://discordapp.com/developers/docs/topics/oauth2#authorization-code-grant
    and
    https://discordapp.com/developers/docs/topics/oauth2#shared-resources
     
    #2 ForbiddenSoul, Mar 6, 2020
    Last edited: Mar 6, 2020
    • Like Like x 1
    • Useful Useful x 1

  3. Thank you for the reply.

    Everything I've seen about discord webhooks no token is required, I should also mention I'm not creating a bot just sending a message so I didnt think a token was needed and when creating the webhook no token is given.
     
    • Useful Useful x 1
  4. I'll leave this open for now and i will post the class i made (later) in case others want to do this, and i'm sure people will have plenty of advice on how to improve it.
     
    • Useful Useful x 1