How do I check if a video exists on YouTube, in client side

后端 未结 6 1942
攒了一身酷
攒了一身酷 2021-01-12 16:45

I am doing validation for my Youtube url text field.

I need to check, if the Youtube url does not exist I should throw error, I followed this answer an

6条回答
  •  清歌不尽
    2021-01-12 16:57

    For those who are looking for a solution using the V3 API, you can do the following:

    var videoID = 'the_youtube_video_id';
    $.getJSON('https://www.googleapis.com/youtube/v3/videos?id=' + videoID 
               + "&key=INSERT_YOUR_API_KEY_HERE&part=COMMA_DELIMITED_VALUE", 
      function (data, status, xhr) {               
        if (data.items.length > 0)
            alert('It is there!')
        else
            alert('Are you sure you about the Id?');
    
        console.log(status);
        console.log(xhr);
    }).error(function (xhr, errorType, exception) {
        var errorMessage = exception || xhr.statusText || xhr.responseText;
        alert(errorMessage);
    });
    

    For a list of valid parts you may visit the doc page here.

提交回复
热议问题