我是新手NodeJS,几天前刚发现Youtube API v3。我正在尝试上传3个视频到我的频道,但是效果不好。以下是错误图像:

3个视频包括: 800mb、600mb和100mb。只有视频100mb是可以的,其他的都失败了。但是如果我上传一个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"
}
}请告诉我,问题出在哪里。以及如何修复它?谢谢大家
发布于 2019-04-17 18:43:44
对于您调用credetials.json文件的属性的相同方式,可能值得对video.json文件采用类似的方法。
//即您已使用凭据执行了以下操作...const凭证= readJson(${__dirname}/example/credentials.json);
//因此,使用您的Video.json信息....var视频= readJson(`${__dirname}/videos.json");
//还要仔细检查文件路径
另一个含义可能是您是否可以在localhost/5000上上传多个视频
https://stackoverflow.com/questions/40104721
复制相似问题