最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Create YouTube Playlist using NodeJS - Stack Overflow

programmeradmin2浏览0评论

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: .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 acplish this is through this JavaScript link: .js

And I have added this method from the above code to the nodejs-quickstart.js to try to acplish 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?

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./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 acplish this is through this JavaScript link: https://github./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 acplish 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?

Share Improve this question edited Aug 10, 2017 at 2:05 Roger99 asked Aug 7, 2017 at 19:13 Roger99Roger99 1,0122 gold badges14 silver badges43 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4 +25

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

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./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

发布评论

评论列表(0)

  1. 暂无评论