【发布时间】:2018-05-18 16:34:07
【问题描述】:
我正在尝试:
- 获取 YouTube 实时聊天消息列表
- 获取所有 YouTube 消息和 ID
- 如果 youtube 消息是某个值,则获取 id 并删除消息。
我一直在删除消息。
到目前为止,我已经通过 HTTP 请求了一个 JSON 消息列表,获取了结果,并将其解析为消息的 ID 和名称。
我不确定如何删除该消息。我可以获得消息列表,但我无法删除任何内容,因为我没有使用 oAuth(2) 登录。我不确定如何轻松登录。我尝试使用 YouTube API 网页中的示例,但它们需要一个又一个的 API,然后这个 API 需要更多的 API。我尝试全部安装,但找不到文件。
我想知道是否有一种方法可以轻松地使用 HTTP 进行身份验证,而无需安装无数 API 来找到我需要的那两个或三个方法。
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 API 的 Java 快速入门。它展示了如何使用 OAuth2 进行身份验证,而无需在 API 上安装 API。第 1 步:Turn on the YouTube Data API。第二步:Prepare the project 第三步:Set up the sample 第四步:Run the sample
-
谢谢@noogui。这对我帮助很大。将其发布为答案!
-
如果你不是那么我会的,@noogui。谢谢!
标签: java youtube youtube-api live-streaming