使用 YouTube 数据查找视频的许可类型 API v3

Find the licence type of a video using YouTube data API v3

我正在尝试编写一个 JavaScript 代码,可以通过在 var 中输入视频的 ID 来检测视频的许可证。因此,我查看了 youtube API 的官方页面,然后想出了这个 url

https://www.googleapis.com/youtube/v3/videos?id=gwLej8heN5c&part=status&key= { 我的 API 密钥 }

这将 return 这个结果

    "uploadStatus": "processed",
    "privacyStatus": "public",
    "license": "creativeCommon",
    "embeddable": true,
    "publicStatsViewable": true

这对我来说很好,因为我得到了 "license: creativeCommon" 但是我不能将它带入 JavaScript 函数。 注意:我尝试使用 iframe,但是即使在代码中添加 &output=embed 之后它也没有在 iframe 中显示任何内容。

为了说明这一点,我打算用它来做:

var result = // somehow to get the result from the API

function licensetype() { 

var cheack = result.search("creativeCommon");

if ( cheack = -1 ) {

document.write("This video is not a creative Common");
} else {

document.write (" This video is a creativeCommon" );

那么有没有办法可以在 javascript 变量中得到上面显示的结果?

根据我对你问题的理解,你可能想试试:

var check = result.license;

switch (check) {
    case undefined:
        // Property 'license' not in result
        break;
    case "creativeCommon":
        // Found
        break;
    default:
        // 'license' property exists but is not 'creativeCommon'
        break;
}

不太熟悉 Youtube API,但我假设查询返回的结果可以解析为 JSON 对象。

希望对您有所帮助。