Home > Blockchain >  Youtube api v3 returning videos for wrong channel when given channel id
Youtube api v3 returning videos for wrong channel when given channel id

Time:12-28

I am trying to write a node.js script that will retrieve all videos uploaded by a user's channel.

So far my script will query a channel for their videos by receiving a channelId as input. It will then send the first video id to a different function, which will print the video title.

I went into the YouTube settings for my account, and copied the channel ID to use in my script.

However when I run my script, it returns videos uploaded by a different channel.

There are no errors, all my requests complete just fine, and I've double checked my channel ID is correct, but for some reason it always returns videos uploaded by NBC sports? Is there something wrong with my getVids() function causing the wrong channel to get queried?

const axios = require('axios');
const { resolve } = require('path');

var apiKey = '~~~got api key from google cloud~~~'
var channelId = 'UCyFpI1Ft7ZhxA06je0R3MDg'
//^^ my channel id, see: https://youtube.com/channel/UCyFpI1Ft7ZhxA06je0R3MDg

getVids(channelId)

//get all videos for channel
async function getVids() {
    const options = {
        method: 'GET',
        url: `https://youtube.googleapis.com/youtube/v3/search? part=snippet&maxResults=50&id=${channelId}&key=${apiKey}`
    };
    const result = await axios(options);
    let videoId = result.data.items[0].id.videoId
    let videoData = await getVideoData(videoId)
    console.log(videoData)
}

//get video metadata (title, date uploaded, etc..) for video id 
async function getVideoData(videoId){
    console.log(`getting video data for ${videoId}`)
    const options = {
        method: 'GET',
        url: `https://youtube.googleapis.com/youtube/v3/videos?part=snippet&id=${videoId}&key=${apiKey}`
    };
    const result = await axios(options);
    return('video title=',result.data.items[0].snippet.title)
}

The video title that get's printed out is an NBC sports video, see my script output below:

getting video data for lLEZnKAobD8
TVF's Sixer - New Web Series | Episode 3 - Match Day Live

CodePudding user response:

Replacing:

url: `https://youtube.googleapis.com/youtube/v3/search? part=snippet&maxResults=50&id=${channelId}&key=${apiKey}`

to

url: `https://youtube.googleapis.com/youtube/v3/search?part=snippet&maxResults=50&channelId=${channelId}&key=${apiKey}`

Solves your problem. Note the space removal and the switch from id to channelId, as id isn't a filter for the YouTube Data API v3 Search: list endpoint.

  • Related