如何使用 Java 删除直播聊天消息

How to delete livestream chat message using Java

我正在尝试:

我一直在删除消息。

到目前为止,我已经使用 HTTP 请求了一个 JSON 消息列表,获取结果并将其解析为消息的 ID 和名称。

我不确定如何删除邮件。我可以获得消息列表,但我无法删除任何内容,因为我没有使用 oAuth(2) 登录。我不确定如何轻松登录。我尝试使用 YouTube API 网页中的示例,但它们在 API 之后需要 API,然后这个 API 需要更多 API。我尝试全部安装它们,但找不到文件。

我想知道是否有一种方法可以轻松地使用 HTTP 进行身份验证,而无需安装无数 APIs 来找到我需要的那两种或三种方法。

import java.io.FileOutputStream;
import java.io.FileReader;
import java.net.URL;
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];
                System.out.println(id);
                System.out.println(msg);
            }
    }

    void deleteChatMessage(String id) {

    }

    HashMap<Integer, String[]> listChatMessages() {
        HashMap<Integer, String[]> messages = new HashMap<>();
        try {
            URL response = new URL("https://www.googleapis.com/youtube/v3/liveChat/m"
                    + "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);
            fos.close();

            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) {
            ex.printStackTrace();
        }
        return messages;
    }
}

查看 Youtube Java 快速入门 API。它展示了如何使用 OAuth2 进行身份验证而无需在 APIs 上安装 APIs。

第 1 步:打开 YouTube 数据 API。

第 2 步:准备项目

第 3 步:设置示例

第 4 步:运行 样本

(最初由@noogui 于 12 月 6 日作为评论发布)