【发布时间】:2017-02-27 12:28:05
【问题描述】:
我是 NodeJS 新手,3 天前才发现 Youtube API v。我正在尝试将 3 个视频上传到我的频道,但效果不佳。下面是错误图片:
3 个视频包括:800mb、600mb 和 100mb。只有 100mb 的视频是可以的,其他的都失败了。 但如果我上传 1 个视频有 800mb,没关系,没问题。我不知道原因。这是我的代码:
"use strict";
const Youtube = require("./lib")
, fs = require("fs")
, readJson = require("r-json")
, Lien = require("lien")
, Logger = require("bug-killer")
, opn = require("opn")
, prettyBytes = require("pretty-bytes")
;
// Import list videos
var listVideos = require("./Videos.json");
console.log(typeof listVideos);
// Declare Result
var result;
// I downloaded the file from OAuth2 -> Download JSON
const CREDENTIALS = readJson(`${__dirname}/example/credentials.json`);
// Init lien server
let server = new Lien({
host: "localhost"
, port: 5000
});
// Authenticate
// You can access the Youtube resources via OAuth2 only.
// https://developers.google.com/youtube/v3/guides/moving_to_oauth#service_accounts
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"]
}));
// Handle oauth2 callback
server.addPage("/oauth2callback", 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. Check out the logs in the terminal.");
for (var video in listVideos) {
console.log(listVideos[video].title);
result = UploadYoutube(listVideos[video].title, listVideos[video].description, listVideos[video].tags,
listVideos[video].fileName);
}
setInterval(function () {
Logger.log(`${prettyBytes(result.req.connection._bytesDispatched)} bytes uploaded.`);
}, 250);
});
});
function UploadYoutube (myTitle, myDescription, myTags, myFileLocation) {
var req = Youtube.videos.insert({
resource: {
// Video title and description
snippet: {
title: myTitle
, description: myDescription
, tags: myTags
}
// I don't want to spam my subscribers
, status: {
privacyStatus: "private"
}
}
// This is for the callback function
, part: "snippet,status"
// Create the readable stream to upload the video
, media: {
body: fs.createReadStream(myFileLocation)
}
}, (err, data) => {
console.log("Done.");
process.exit();
});
return req;
}
这是我的 json 文件,其中包含 nodejs 文件中使用的视频信息:
{
"1" : {
"title" : "Quang Cao Hai Vinamilk Con Bo Cuoi",
"description" : "Quang Cao Cho Be, Quang Cao Cho Be An Ngon",
"tags" : ["Quang Cao Cho Be", "Quang Cao Cho Be An Ngon", "Clip Quang Cao"],
"fileName" : "Quang Cao Hai Vinamilk Con Bo Cuoi.mp4"
},
"2" : {
"title" : "Clip Ngan Vui Nhon",
"description" : "Clip Ngan Vui Nhon, Clip Ngan Vui Nhon Minion",
"tags" : ["Clip Ngan Vui Nhon", "Clip Ngan Vui Nhon Minion"],
"fileName" : "Clip Ngan Vui Nhon.mp4"
},
"3" : {
"title" : "Phim Quang Cao Ngan",
"description" : "Phim Quang Cao Ngan, Phim Quang Cao Ngan Hay Nhat",
"tags" : ["Phim Quang Cao Ngan", "Phim Quang Cao Ngan Hay Nhat"],
"fileName" : "Phim Quang Cao Ngan.mp4"
}
}
请告诉我,问题出在哪里。以及如何解决?谢谢大家
【问题讨论】:
-
请帮我修复它
-
您是否验证了您的频道?
-
是的,我记得我没有验证频道以上传超过 15 分钟的视频 :)) 谢谢
-
我们都是越南人。不客气。
-
是的,我验证了频道,并且可以上传视频 > 15 分钟。但是问题是一样的,我的代码有什么问题:(