Search code examples
youtube-apiyoutube-data-api

How to get livestream chat messages from YouTube? It looks like it's broken


I read the documentation page very attentively, but it won't work.

I'm trying to fetch posts from a stream, but I keep getting an error that the pageToken is invalid. Although this parameter is optional and can only be obtained after receiving a list of messages.

Documentation page

It is not working even on documentation page. To check - you can use 4xDzrJKXOOY id. Here is query:

curl \
  'https://youtube.googleapis.com/youtube/v3/liveChat/messages?liveChatId=4xDzrJKXOOY&part=snippet&key=[YOUR_API_KEY]' \
  --header 'Authorization: Bearer ACCESS TOKEN' \
  --header 'Accept: application/json' \
  --compressed

And here's what I get:

{
  "error": {
    "code": 400,
    "message": "page token is not valid.",
    "errors": [
      {
        "message": "page token is not valid.",
        "domain": "youtube.liveChat",
        "reason": "pageTokenInvalid"
      }
    ]
  }
}

Video id is absolutely valid, you can check it here.


Solution

  • Your liveChatId is incorrect, you should use the value activeLiveChatId returned by YouTube Data API v3 Videos: list endpoint with part=liveStreamingDetails.

    In your case it would be Cg0KCzR4RHpySktYT09ZKicKGFVDU0o0Z2tWQzZOcnZJSTh1bXp0ZjBPdxILNHhEenJKS1hPT1k and retrieving the associated messages with:

    https://youtube.googleapis.com/youtube/v3/liveChat/messages?liveChatId=Cg0KCzR4RHpySktYT09ZKicKGFVDU0o0Z2tWQzZOcnZJSTh1bXp0ZjBPdxILNH&part=snippet&key=[YOUR_API_KEY]

    works fine.