I am using the googleapis
npm package, as described in the official documentation. However, in both cases (using the insert function with callback AND using the insert function as an asynchronous function) I am having issues.
When using the callback function, the code is executed asynchronously and the upload function starts after I try to log the response. When using the insert function as an asynchronous function, the upload completes, but there is no response logged. In fact, nothing is logged at all! Somehow we never reach the logger.log
for Upload Response
…
I need the response to be able to read potential errors and other information. Below is the asynchronous use of the insert function in my upload function, as it is in my code. OAuth has been tested using the people
API and is working fine. I have ended up with 2 videos in my YouTube dashboard that are stuck on infinitely processing
, so something is working?
async function upload(
oauth2Client,
videoInfo,
thumbInfo,
publishAt,
title,
description,
notifySubscribers
) {
let result = {};
// this await is needed because the insert function can optionally return a promise
// which is what we want, ignore the underline in your IDE
try {
const res = await youtube.videos.insert(
{
auth: oauth2Client,
part: "id,snippet,status",
notifySubscribers,
requestBody: {
snippet: {
title,
description,
},
status: {
privacyStatus: "private",
publishAt,
},
},
media: {
body: fs.createReadStream(videoInfo.path),
mimeType: videoInfo.mime,
},
},
{
onUploadProgress: (evt) => {
const progress = Math.round((evt.bytesRead / info.length) * 100);
// readline.clearLine(process.stdout, 0);
// readline.cursorTo(process.stdout, 0, null);
// process.stdout.write(`${progress}% complete`);
logger.log(
"debug",
`Uploading file ${basename(filePath)} ${progress}%`
);
},
}
);
logger.log("sensitive", "== Upload Response ==", res.data);
result["data"] = res.data;
} catch (error) {
logger.log("error", "The API is not doing API things", error);
result["error"] = "Possibly rate limited";
}
return result;
}