getId() on a LiveChatMessage returning null — YT Data API v3

I am trying to get the ID of a LiveChatMessage with the method getId(). It for some reason returns null. Can somebody help me?

Here is part of my code now.

LiveChatMessageListResponse response = youtube
                            .liveChatMessages()
                            .list(liveChatId, "snippet, authorDetails")
                            .setPageToken(nextPageToken)
                            .setFields(LIVE_CHAT_FIELDS)
                            .execute();

List<LiveChatMessage> messages = response.getItems();
for (int i = 0; i < messages.size(); i++) {
    LiveChatMessage message = messages.get(i);
    final String[] full = new String[2];
    System.out.println("ID: " + message.getId());

Here are my LIVE_CHAT_FIELDS.

private static final String LIVE_CHAT_FIELDS =
    "items(authorDetails(channelId,displayName,isChatModerator,isChatOwner,isChatSponsor,"
        + "profileImageUrl),snippet(displayMessage,superChatDetails,publishedAt)),"
        + "nextPageToken,pollingIntervalMillis";

Answer

Your LIVE_CHAT_FIELDS do not include the ID.

Include the ID in items(:

private static final String LIVE_CHAT_FIELDS =
    "items(id,authorDetails(channelId,displayName,isChatModerator,isChatOwner,isChatSponsor,"
        + "profileImageUrl),snippet(displayMessage,superChatDetails,publishedAt)),"
        + "nextPageToken,pollingIntervalMillis";

Leave a Reply

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