【问题标题】:Create YouTube Playlist using NodeJS使用 NodeJS 创建 YouTube 播放列表
【发布时间】:2018-01-15 04:30:24
【问题描述】:

我正在尝试使用 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 方法中没有发生身份验证,并且由于 nodeJS 快速入门示例中不存在/未提及“gapi”和“client”。有人可以帮忙把这个 JS 方法翻译成 nodeJS 吗?

【问题讨论】:

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


    【解决方案1】:

    对于nodejs,

    我建议你使用 Google 开发的这些 nodeJS 模块

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

    考虑以下 sn-ps创建一个 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;
          ...
    }
    

    【讨论】:

    • 我已经做了所有这些,你发布的链接与我发布的链接相同
    • 我已经看过这个页面并且我已经完成了所有这些,我想要完成的内容在文档中没有详细说明
    • @Roger99 我已经编辑了答案。请检查,我已经显示了比较sn-p。希望对您有所帮助。
    【解决方案2】:

    如果你想使用纯 Nodejs,你应该使用 google api nodejs client 并使用这个 sample usage 然后跟随 documentation to insert playlist

    当然你需要authentication process too

    在开始整个进度之前,请确保您已通过控制台/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/

    【讨论】:

    • 您的“插入播放列表的文档”链接在代码示例中没有 nodejs 选项,这是我的主要问题
    • 查看示例用法第53行,你会发现youtube.playlists.list..只需将其更改为youtube.playlists.insert并学习文档
    • @Roger99 想要清楚比较 javascriptnodejs sn-p
    • @BharathvajGanesan:你提供对比,我提供nodejs代码
    • @BharathvajGanesan 和 StefansArya 您是否有机会修改此代码以显示在创建播放列表时将随机视频添加到该播放列表中的样子?
    猜你喜欢
    • 2016-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-16
    • 1970-01-01
    • 2018-04-09
    • 2018-07-24
    • 1970-01-01
    相关资源
    最近更新 更多