使用NodeJS创buildYouTube播放列表

我正在尝试使用NodeJS服务器创buildYouTube播放列表。 我已经按照这个链接看到了NodeJS快速启动Oauth的说明: 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); } }); } 

我现在试图通过我的服务器来创build一个播放列表,但唯一的参考是通过这个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”和“client”不存在/在nodeJS快速入门示例中没有提到。 有人可以帮助翻译这个JS方法到nodeJS吗?

如果你想使用纯粹的Nodejs,你应该使用谷歌API的nodejs客户端,并使用此示例用法,然后按照文档插入播放列表

当然你也需要authentication过程

在开始整个进程之前,请确保已经通过Console / SSH 将google apis安装到了项目文件夹中

样品

控制台: npm install googleapis lien --save

激活您的YouTube数据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,

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

 npm install googleapis --save npm install google-auth-library --save 
  1. googleapis
  2. 谷歌authentication-库

考虑下面的代码片段创build一个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; ... }