Search code examples
pythonyoutube-data-apigoogle-api-client

Response Payload incorrect using Youtube Data API


I'm trying to use Youtube's Data API to get some info on various channels, but when I run my build:

youtube = build('youtube', 'v3',developerKey=api_key)


request = youtube.channels().list(
    part = "statistics",
    forUsername = "tonetalks"
)
response = request.execute()
print(response)

It returns this:

{'kind': 'youtube#channelListResponse', 'etag': 'RuuXzTIr0OoDqI4S0RU6n4FqKEM', 'pageInfo': {'totalResults': 0, 'resultsPerPage': 5}}

Any thoughts how to fix this ?


Solution

  • The forUsername is incorrect. If you're looking for this "tonetalks" channel, you'll have to use its id UCfP8rCe_fAITriqI3UPYF0Q (from the above channel URL):

    request = youtube.channels().list(
        part = "statistics",
        id = "UCfP8rCe_fAITriqI3UPYF0Q",
    )
    
    {
      "kind": "youtube#channelListResponse",
      "etag": "5gs56_i4Xd_fQ4A1OkQEnWWnX7A",
      "pageInfo": {
        "totalResults": 1,
        "resultsPerPage": 5
      },
      "items": [
        {
          "kind": "youtube#channel",
          "etag": "m7gogJwH4TshrBX4PCiuFP5MsJI",
          "id": "UCfP8rCe_fAITriqI3UPYF0Q",
          "statistics": {
            "viewCount": "6033544",
            "subscriberCount": "81200",
            "hiddenSubscriberCount": false,
            "videoCount": "229"
          }
        }
      ]
    }