youtube-api
A Node.JS module, which provides an object oriented wrapper for the Youtube v3 API.
Installation
$ npm i youtube-api
Example
You may be interested to download this test application
and play with the YouTube API resources there. Below you see an example how to use the library.
var Youtube = require("youtube-api")
, Fs = require("fs")
, ReadJson = require("r-json")
, Lien = require("lien")
, Logger = require("bug-killer")
, Opn = require("opn")
;
const CREDENTIALS = ReadJson("./credentials.json");
var server = new Lien({
host: "localhost"
, port: 5000
});
var stdIn = ReadLine.createInterface({
input: process.stdin
, output: process.stdout
});
var 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.page.add("/oauth2callback", function (lien) {
Logger.log("Trying to get the token using the following code: " + lien.search.code);
oauth.getToken(lien.search.code, function(err, tokens) {
if (err) { lien(err, 400); return Logger.log(err); }
oauth.setCredentials(tokens);
Youtube.videos.insert({
resource: {
snippet: {
title: "Testing YoutTube API NodeJS module"
, description: "Test video upload via YouTube API"
}
, status: {
privacyStatus: "private"
}
}
, part: "snippet,status"
, media: {
body: Fs.createReadStream("video.mp4")
}
}, function (err, data) {
if (err) { return lien.end(err, 400); }
lien.end(data);
});
});
});
Documentation
The official Youtube documentation is a very useful resource.
If you have any questions, just open an issue.
Authentication
OAuth (Access Token)
Youtube.authenticate({
type: "oauth"
, token: "your access token"
});
OAuth (Refresh Token)
Youtube.authenticate({
type: "oauth"
, refresh_token: "your refresh token"
, client_id: "your client id"
, client_secret: "your client secret"
, redirect_url: "your refresh url"
});
Server Key
Only for requests that don't require user authorization (certain list operations)
Youtube.authenticate({
type: "key"
, key: "your server key"
});
How to contribute
Have an idea? Found a bug? See how to contribute.
License
KINDLY © Ionică Bizău–The LICENSE file contains
a copy of the license.