【发布时间】:2017-12-01 06:27:01
【问题描述】:
我正在关注有关使用 Node.Js (here is the link) 上传 Youtube 视频的教程,效果很好。唯一的问题是,完成脚本后,我找不到我上传的视频。这是我正在使用的脚本(正在上传的视频称为 video.mp4):
const Youtube = require("youtube-api");
const fs = require("fs");
const readJson = require('r-json');
const Lien = require("lien");
const Logger = require("bug-killer");
const opn = require("opn");
const prettyBytes = require("pretty-bytes");
const CREDENTIALS = readJson(`${__dirname}/credentials.json`);
// initialize Lien server
let server = new Lien({
host: "localhost",
port: 3000
});
let oauth = Youtube.authenticate({
type: "oauth",
client_id: CREDENTIALS.web.client_id,
client_secret: CREDENTIALS.web.client_secret,
redirect_url: CREDENTIALS.web.redirect_uris[0]
});
opn(oauth.generateAuthUrl({
access_type: "offline",
scope: ["https://www.googleapis.com/auth/youtube.upload"]
}));
server.addPage("/response", lien => {
Logger.log("Trying to get the token using the following code: " + lien.query.code);
oauth.getToken(lien.query.code, (err, tokens) =>{
if (err){
lien.lien(err, 400);
return Logger.log(err);
}
Logger.log("Got the tokens");
oauth.setCredentials(tokens);
lien.end("The video is being uploaded.");
var req = Youtube.videos.insert({
resource:{
snippet:{
title: "Testing Youtube API",
description: "testing the youtubes"
},
status: {privacyStatus: "private"}
},
part: "snippet, status",
media: {
body: fs.createReadStream("video.mp4")
}
},(err, data) => {
console.log("Done." + data);
process.exit();
});
setInterval(function(){
Logger.log(`${prettyBytes(req.req.connection._bytesDispatched)} bytes uploaded.`);
}, 250);
});
});
我通过 console.log 得到正确的输出,告诉我视频已上传:
info Trying to get the token using the following code: XXXXXXXXXX
info Got the tokens
info 263 kB bytes uploaded.
info 384 kB bytes uploaded.
info 384 kB bytes uploaded.
info 384 kB bytes uploaded.
Done.
【问题讨论】:
-
@JaromandaX 我认为这是一个合理的问题,我相信其他人也遇到过类似的问题,特别是考虑到我链接到的代码是 - 据我所知 - 唯一的示例代码它演示了通过 node.js 的
youtube-api包上传到 Youtube 的视频。检查我的答案以尝试解决此问题。 -
我知道这是一个合理的问题(如果我不认为它是,我会标记为关闭) - 第一部分是一个笑话,第二部分是我的诚实意见
-
@JaromandaX 明白了。花了一个晚上一遍又一遍地上传相同的玩具机器人视频后有点暴躁......
标签: javascript node.js video youtube