Create a YouTube playlist with NodeJS

I am trying to create a youtube playlist using a NodeJS server. I followed the NodeJS quick start instructions for Oauth, as seen at this link: https://github.com/youtube/api-samples/blob/master/javascript/nodejs-quickstart.js

From this link, I was also able to access the channel information using the following method:

function getChannel(auth) {
  var service = google.youtube('v3');
  service.channels.list({
    auth: auth,
    part: 'snippet,contentDetails,statistics',
    forUsername: 'GoogleDevelopers'
  }, function(err, response) {
    if (err) {
      console.log('The API returned an error: ' + err);
      return;
    }
    var channels = response.items;
    if (channels.length == 0) {
      console.log('No channel found.');
    } else {
      console.log('This channel\ ID is %s. Its title is \'%s\', and ' +
                  'it has %s views.',
                  channels[0].id,
                  channels[0].snippet.title,
                  channels[0].statistics.viewCount);
    }
  });
}

Right now I'm trying to create a playlist through my server, but the only link to how to do this is this JavaScript link: https://github.com/youtube/api-samples/blob/master/javascript/playlist_updates.js

And I added this method from the above code in nodejs-quickstart.js to try to execute this:

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;
    if (result) {
      playlistId = result.id;
      $('#playlist-id').val(playlistId);
      $('#playlist-title').html(result.snippet.title);
      $('#playlist-description').html(result.snippet.description);
    } else {
      $('#status').html('Could not create playlist');
    }
  });
}

NodeJS, JS- , "gapi" "client" / quickstart nodeJS. - JS nodeJS?

+4
2

Nodejs, google api nodejs ,

, ,

, , google apis /SSH

: npm install googleapis lien --save

API Youtube

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);
            }
        });
    });
});

script http://localhost:5000/

+3

nodejs,

nodeJS , Google.

npm install googleapis --save
npm install google-auth-library --save

: Youtube

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;
      ...
}
+3

Source: https://habr.com/ru/post/1683190/


All Articles