'Google app script Youtube service fails to insert live chat message

I wrote a function to insert a message in a youtube live chat using Youtube service in google app script. However the function throws an error

GoogleJsonResponseException: API call to youtube.liveChatMessages.insert failed with error: Request contains an invalid argument. at InsertLiveChatMessage (Code:122:41) at __GS_INTERNAL_top_f...

This is the function that I've tried running from the editor

function InsertLiveChatMessage(live_chat_id="Cg0KC0N5XzYtX1hVVy1jKicKGFVDdDR0LWplWTg1SmVnTWxaLUU1VVd0QRILQ3lfNi1fWFVXLWM", message="message")
{
  let response = {};
  let resource = {
    "snippet": {
      "liveChatId": live_chat_id,
      "type": "textMessageEvent",
      "textMessageDetails": {
        "messageText": message
      }
    }
  };

  try
  {
    response = YouTube.LiveChatMessages.insert(resource, "snippet");
  }
  catch(e)
  {
    response["error"] = "Something went wrong";
  }

  console.log(response);
}

I had enabled Youtube data api service and granted permissions before running the above function. I borrowed resource object from Google API explorer. Can someone point if it a google app script problem or something to do with my code.



Solution 1:[1]

I thought it would help someone debug this issue if in a similar state.

There is nothing wrong with the script code itself. However google doesn't allow sending chats via api unless you see the option to send message through YouTube website/app. The account I was using in this case was primarily used for watching videos but I had never once streamed a single live stream or participated in chat of one using that Google account. So it's more of a permission issue.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Monu Yadav