使用NodeJS创建YouTube播放列表

时间:2017-08-07 19:13:01

标签: javascript node.js youtube-api youtube-data-api

我正在尝试使用NodeJS服务器创建youtube播放列表。我已按照此链接中的Oauth的NodeJS快速入门说明进行操作:https://github.com/youtube/api-samples/blob/master/javascript/nodejs-quickstart.js

通过此链接,我还可以使用以下方法访问频道信息:

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

我现在正尝试通过我的服务器创建一个播放列表,但唯一的参考方法是通过以下JavaScript链接:https://github.com/youtube/api-samples/blob/master/javascript/playlist_updates.js

我已将上述代码中的此方法添加到nodejs-quickstart.js以尝试实现:

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方法中没有auth发生,因为" gapi"和"客户"在nodeJS快速入门示例中没有提及/没有提及。有人可以帮助将这个JS方法转换为nodeJS吗?

2 个答案:

答案 0 :(得分:3)

对于nodejs,

我建议您使用 Google 开发的 nodeJS模块

npm install googleapis --save
npm install google-auth-library --save
  1. googleapis
  2. google-auth-library
  3. 中考虑以下片段创建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;
          ...
    }
    

答案 1 :(得分:3)

如果您想使用纯Nodej,请使用google api nodejs client并使用此sample usage,然后按照documentation to insert playlist

当然,您需要authentication process too

在开始整个进度之前,请确保通过Console / SSH将installed google apis放入项目文件夹

<强>示例

控制台: 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);
            }
        });
    });
});

运行脚本后,只需通过您喜欢的浏览器转到http://localhost:5000/

相关问题