2

next: YouTube live chat in Python Flask: pageTokenInvalid error
I want to retrieve my YouTube live stream chat's using the YouTube Data API in Python.

On the official doc web page of the YouTube Live Streaming API I used Try this API:

# Sample Python code for youtube.liveChatMessages.list
# See instructions for running these code samples locally:
# https://developers.google.com/explorer-help/guides/code_samples#python

import os

import google_auth_oauthlib.flow
import googleapiclient.discovery
import googleapiclient.errors

scopes = ["https://www.googleapis.com/auth/youtube.readonly"]

def main():
    # Disable OAuthlib's HTTPS verification when running locally.
    # *DO NOT* leave this option enabled in production.
    os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"

    api_service_name = "youtube"
    api_version = "v3"
    client_secrets_file = "YOUR_CLIENT_SECRET_FILE.json"

    # Get credentials and create an API client
    flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
        client_secrets_file, scopes)
    credentials = flow.run_console()
    youtube = googleapiclient.discovery.build(
        api_service_name, api_version, credentials=credentials)

    request = youtube.liveChatMessages().list(
        liveChatId="YOUR_CHAT_ID",
        part="snippet,authorDetails"
    )
    response = request.execute()

    print(response)

if __name__ == "__main__":
    main()

But the I got response was:

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

What is the pageTokenInvalid error and how can I get the pageToken?

alskfl222
  • 21
  • 3
  • Did you look this up? You shouldn't need to specify a pageToken for the first page. The first page request will return a nextPageToken that you pass to get the next page. You're sure your chat ID is accurate? – Tim Roberts Feb 25 '21 at 06:36
  • @TimRoberts in google api explorer, i can try it like that – alskfl222 Feb 25 '21 at 06:40
  • request['nextPageToken'] does not exist? – r-beginners Feb 25 '21 at 06:41
  • @r-beginners Yes just error – alskfl222 Feb 25 '21 at 06:44
  • @alskfl222: Do copy-paste that Python code into a file; then run that under a command like `python your_script_file.py` (on Windows: under command prompt window; on GNU/Linux: under a terminal window). It should work OK, if you're gonna get the OAuth authentication/authorization flow to run successfully to completion. Don't forget to have your client secrets JSON file (that you'll obtain upon creating a project on [Google Developers Console](https://console.developers.google.com/)) within the folder containing `your_script_file.py`. – stvar Feb 25 '21 at 08:33
  • Thanks @stvar, but I think I already try that similarly. I will post another one – alskfl222 Feb 25 '21 at 09:00

0 Answers0