How to delete livestream chat message using Java

I am trying to:

  • Get a list of YouTube live chat messages
  • Get all YouTube messages and ID
  • If youtube message is a certain value, then take the id and delete the message.

I am stuck on deleting the message.

So far I have requested a JSON list of messages with HTTP, taken the result, and parsed it into the ID and name of the message.

I’m not sure how to delete the message. I can get a list of messages but I cannot delete anything because I am not logged in with oAuth(2). I’m not sure how to log in easily. I tried using examples from the YouTube API webpage but they require API after API and then this one API required more APIs. I tried installing them all but am having trouble finding the files.

I am wondering if there is a way to authenticate easily with HTTP without having to install innumerable APIs in order to find those two or three methods that I need.

import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.util.HashMap;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

class Game {
    void main() {
        //while (true) {
            HashMap<Integer, String[]> messages = listChatMessages();

            for (int i = 0; i < messages.size(); i++) {
                String[] full = messages.get(i);
                String id = full[0];
                String msg = full[1];

    void deleteChatMessage(String id) {


    HashMap<Integer, String[]> listChatMessages() {
        HashMap<Integer, String[]> messages = new HashMap<>();
        try {
            URL response = new URL(""
                    + "essages?liveChatId=[snip]&part=sn"
                    + "ippet&key=[snip]&maxResults=200&pageToken=[snip]");
            ReadableByteChannel rbc = Channels.newChannel(response.openStream());
            FileOutputStream fos = new FileOutputStream("messages.json");
            fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);

            JSONParser parser = new JSONParser();
            Object obj = parser.parse(new FileReader("messages.json"));
            JSONObject jObj = (JSONObject) obj;
            JSONArray jArr = (JSONArray) jObj.get("items");
            for (int i = 0; i < jArr.size(); i++) {
                JSONObject msg = (JSONObject) jArr.get(i);
                JSONObject snippet = (JSONObject) msg.get("snippet");
                JSONObject txtDetails = (JSONObject) snippet.get("textMessageDetails");
                String[] full = new String[2];
                full[1] = (String) txtDetails.get("messageText");
                full[0] = (String) msg.get("id");

                messages.put(i, full);

        } catch (Exception ex) {
        return messages;


Check the Java Quickstart for Youtube API. It shows how to authenticate with OAuth2 without needing to install APIs upon APIs.

Step 1: Turn on the YouTube Data API.

Step 2: Prepare the project

Step 3: Set up the sample

Step 4: Run the sample

(Originally posted as a comment by @noogui on Dec 6)

Leave a Reply

Your email address will not be published. Required fields are marked *