I am trying to:
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.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;
}
}
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)