Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create YouTube Playlist using NodeJS

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

From this link, I have also been able to access channel information by using the method below:

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\'s ID is %s. Its title is \'%s\', and ' +
                  'it has %s views.',
                  channels[0].id,
                  channels[0].snippet.title,
                  channels[0].statistics.viewCount);
    }
  });
}

I am now attempting to create a playlist through my server, but the only reference to how to accomplish this is through this JavaScript link: https://github.com/youtube/api-samples/blob/master/javascript/playlist_updates.js

And I have added this method from the above code to the nodejs-quickstart.js to try to accomplish that:

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

I am having trouble translating this over to the NodeJS example, since there is no auth happening in the JS method, and since "gapi" and "client" don't exist/aren't mentioned in the nodeJS quickstart example. Could someone help with translating this JS method over to nodeJS?

like image 843
Roger99 Avatar asked Mar 09 '23 09:03

Roger99


1 Answers

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;
      ...
}
like image 70
Bharathvaj Ganesan Avatar answered Mar 11 '23 04:03

Bharathvaj Ganesan