Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用api(captions.download)下载Youtube成绩单_Javascript_Youtube_Google Apis Explorer_Google Api Js Client - Fatal编程技术网

Javascript 如何使用api(captions.download)下载Youtube成绩单

Javascript 如何使用api(captions.download)下载Youtube成绩单,javascript,youtube,google-apis-explorer,google-api-js-client,Javascript,Youtube,Google Apis Explorer,Google Api Js Client,我已经构建了一个javascript代码,可以读取任何Youtube视频转录本(gapi.client.Youtube.captions.download)。auth 2.0工作正常,我在本地web服务器上运行我的应用程序一切正常,问题是当我运行请求时出现错误403:cb=gapi.loaded_0:164 GET 403,我在StackOverflow中没有找到任何解决方案。有什么想法吗 这是我的js文件: const CLIENT_ID = 'My_Client_ID'; const DIS

我已经构建了一个javascript代码,可以读取任何Youtube视频转录本(gapi.client.Youtube.captions.download)。auth 2.0工作正常,我在本地web服务器上运行我的应用程序一切正常,问题是当我运行请求时出现错误403:cb=gapi.loaded_0:164 GET 403,我在StackOverflow中没有找到任何解决方案。有什么想法吗

这是我的js文件:

const CLIENT_ID = 'My_Client_ID';
const DISCOVERY_DOCS = ["https://www.googleapis.com/discovery/v1/apis/youtube/v3/rest"];
const SCOPES = 'https://www.googleapis.com/auth/youtube.readonly';

const authorizeButton = document.getElementById('enter-button');
const signoutButton = document.getElementById('exit-button');
const content = document.getElementById('content');

// default youtube channel
const defaultChannel = 'googledevelopers';

// Load auth2 library
function handleClientLoad(){
    gapi.load('client:auth2', initClient);
}

// Init API client library and set up sing in listeners
function initClient(){
    gapi.client.init({
        discoveryDocs: DISCOVERY_DOCS,
        clientId: CLIENT_ID,
        scope: SCOPES
    }).then(() => {
        // Listen for sing state changes
        gapi.auth2.getAuthInstance().isSignedIn.listen(updateSigninStatus);
        // Handle initial sign in state
        updateSigninStatus(gapi.auth2.getAuthInstance().isSignedIn.get());
        authorizeButton.onclick = handleAuthClick;
        signoutButton.onclick = handleSignouClick;
    });
}

// update UI sign in state changes
function updateSigninStatus(isSignedIn){
    if(isSignedIn){
        authorizeButton.style.display = 'none';
        signoutButton.style.display = 'block';
        content.style.display = 'block';
        getChannel(defaultChannel);
    }else{
        authorizeButton.style.display = 'block';
        signoutButton.style.display = 'none';
        content.style.display = 'none';
    }
}

// Handle Login
function handleAuthClick(){
    gapi.auth2.getAuthInstance().signIn();
}

// Handle Logout
function handleSignouClick(){
    gapi.auth2.getAuthInstance().signOut();
}

// Display channel Data
function showChannelData(data){
    const channelData = document.getElementById('channel-data');
    channelData.innerHTML = data;
}

// Get channel from API
function getChannel(channel){
    gapi.client.youtube.captions.download({
        id: 'guMGyC1tUYAdL3hgBlcGnW4Rt_bBUbtp'
    })
    .then(response => {
        console.log(response);
        const channel = response.result.items[0];
    })
    .catch(err => alert('No Channel By THat Name'));
}
这是我的index.ejs文件:

<!DOCTYPE html>
<html lang="en">
    <head>      
        <title>Your awesome Youtube search engine</title>
        <meta charset="UTF-8" />                    
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="description" content="Awesome videos!" />
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
    </head>
    <body>
        <header>
            <h1 class="w100 text-center"><a href="index.html">YouTube Viral Search</a></h1>
        </header>

<div class="container">
        <p>Login with Google</p>
        <button class="btn green" id="enter-button">Log In</button>
        <button class="btn green" id="exit-button">Log Out</button>
        <br />
        <div id="content">
            <div class="row">
                <div id="channel-data" class="col s12"></div>
            </div>
        </div>
    </div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-rc.2/js/materialize.min.js"></script>
<script src="/javascripts/appYT.js"></script>
    <script async defer src="https://apis.google.com/js/api.js"
      onload="this.onload=function(){};handleClientLoad()"
      onreadystatechange="if (this.readyState === 'complete') this.onload()">
    </script>
    </body>
</html>
enter code here

你很棒的Youtube搜索引擎
使用谷歌登录

登录 注销
在这里输入代码
您可以使用以下代码获取给定视频中的成绩单


注意:我这里有
videoId
zenMEj0cAC4
,但是您可以根据需要更改它

$.ajax({
键入:“获取”,
url:“https://video.google.com/timedtext?type=track&v=zenMEj0cAC4&id=0&lang=en",
跨域:是的,
}).完成(功能(数据){
控制台日志(数据);
获取标题(数据);
});
var解析器,xmlDoc;
var HTML_captions=“”;
//解析AJAX响应并获取标题。
函数getCaption(ajax\u响应){
试一试{
parser=新的DOMParser();
xmlDoc=parser.parseFromString(ajax_响应,“text/xml”);
//log(ajax\u响应);
//log(xmlDoc.getElementsByTagName(“transcript”).length);
if(xmlDoc.getElementsByTagName(“转录本”).length>0){
//循环xmlDoc的结果:
对于(var i=0;i”;
}
}否则{
//循环ajax\u响应的结果;
对于(var i=0;i”;
}
}
document.getElementById(“demo”).innerHTML=“准备字幕…”;
setTimeout(fillData(),2000);
}捕捉(错误){
控制台日志(err);
document.getElementById(“demo”).innerHTML=(“getCaption函数出错-请参阅控制台表单详细信息”);
警报('getCaption函数出错-请参阅控制台窗体的更多详细信息');
}
}
//在HTML“div”控件中填充数据“标题”。
函数fillData(){
试一试{
document.getElementById(“demo”).innerHTML=HTML\u标题;
}捕捉(错误){
控制台日志(err);
document.getElementById(“demo”).innerHTML=(“fillData函数出错-请参阅控制台表单详细信息”);
警报('fillData函数出错-请参阅控制台表单了解更多详细信息');
}
}


正在加载字幕…
有关更多说明,我看到了一条错误消息:权限不足:请求的身份验证范围不足。我不明白!!阅读任何youtube字幕曲目都是公开的,对吗?我的身份验证工作正常Dia,你在单个视频或任何视频中都有此错误吗?我有如果有任何视频,请尝试并测试。无法确定为什么会出现这样的错误。希望它有帮助,如果有,请指出,我将发布它作为答案。好的,我会尝试。那么你不使用gapi吗?@Lydiahalls,你必须激活谷歌chrome中的F12开发者工具,例如“在YouTube上播放视频,然后检查“网络”tab当你按下按钮激活视频上的标题时。你会在那里看到请求。希望它有帮助。对于一些YT视频,“…”和“…”在1-我看网络选项卡时都不起作用。是不是caption.js请求这些url中的一个?2-如果我想使用另一种语言,我可以更改“&lang=en&name=CC%20吗(英语)“to”&lang=fr&name=CC%20(法语)”例如?我真的很感谢你的帮助Mauricio;)它不适用于此视频例如:3-你知道为什么吗?谢谢:)Mauricio你是最好的..现在我知道如何从网络选项卡获取它..非常感谢:)@LeeGee,在你提供的视频ID中,答案是空的。这可能是由于频道本身的原因。