Create YouTube Playlist using NodeJS

匆匆过客 提交于 2019-12-04 11:15:59

If you want to use pure Nodejs, you should use google api nodejs client and use this sample usage then follow the documentation to insert playlist

And of course you will need authentication process too

Before starting the whole progress, make sure you have installed google apis into the project folder through the Console/SSH

Sample

Console: npm install googleapis lien --save

Activate your Youtube Data API

var google = require('googleapis');
var Lien = require("lien");
var OAuth2 = google.auth.OAuth2;

var server = new Lien({
    host: "localhost"
  , port: 5000
});

var oauth2Client = new OAuth2(
  'YOUR_CLIENT_ID',
  'YOUR_CLIENT_SECRET',
  'http://localhost:5000/oauthcallback'
);

var scopes = [
  'https://www.googleapis.com/auth/youtube'
];

var youtube = google.youtube({
  version: 'v3',
  auth: oauth2Client
});

server.addPage("/", lien => {
    var url = oauth2Client.generateAuthUrl({
        access_type: "offline",
        scope: scopes
    });
    lien.end("<a href='"+url+"'>Authenticate yourself</a>");
})

server.addPage("/oauthcallback", lien => {
    console.log("Code obtained: " + lien.query.code);
    oauth2Client.getToken(lien.query.code, (err, tokens) => {
        if(err){
            return console.log(err);
        }

        oauth2Client.setCredentials(tokens);
        youtube.playlists.insert({
            part: 'id,snippet',
            resource: {
                snippet: {
                    title:"Test",
                    description:"Description",
                }
            }
        }, function (err, data, response) {
            if (err) {
                lien.end('Error: ' + err);
            }
            else if (data) {
                lien.end(data);
            }
            if (response) {
                console.log('Status code: ' + response.statusCode);
            }
        });
    });
});

After you run your script, just go to http://localhost:5000/ through your favorite browser

For nodejs,

I suggest you to use these nodeJS modules developed by Google.

npm install googleapis --save
npm install google-auth-library --save
  1. googleapis
  2. google-auth-library

Consider the following snippets to Create a Youtube playlist in

NodeJS

googleapis.discover('youtube', 'v3').execute(function (err, client) {
    var request = client.youtube.playlists.insert(
       { part: 'snippet,status'},
       {
         snippet: {
           title: "hello",
           description: "description"
       },
       status: {
           privacyStatus: "private"
       }
   });
   request.withAuthClient(oauth2Client).execute(function (err, res) {...
});

JavaScript

function createPlaylist() {
   var request = gapi.client.youtube.playlists.insert({
      part: 'snippet,status',
      resource: {
         snippet: {
         title: 'Test Playlist',
         description: 'A private playlist created with the YouTube API'
         },
         status: {
            privacyStatus: 'private'
         }
      }
   });
   request.execute(function(response) {
      var result = response.result;
      ...
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!