使用 NodeJS 创建 YouTube 播放列表 [英] Create YouTube Playlist using NodeJS

查看:31
本文介绍了使用 NodeJS 创建 YouTube 播放列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我现在正在尝试通过我的服务器创建一个播放列表,但有关如何完成此操作的唯一参考是通过此 JavaScript 链接:https://github.com/youtube/api-samples/blob/master/javascript/playlist_updates.js

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

并且我已将上述代码中的此方法添加到 nodejs-quickstart.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');
    }
  });
}

我无法将其转换为 NodeJS 示例,因为在 JS 方法中没有发生身份验证,并且因为gapi"和客户端"不存在/在 nodeJS 快速入门示例中未提及.有人能帮忙把这个 JS 方法翻译成 nodeJS 吗?

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?

推荐答案

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

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

当然,您还需要身份验证过程

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

Before starting the whole progress, make sure you have installed google apis into the project folder through the Console/SSH

示例

控制台: 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/

After you run your script, just go to http://localhost:5000/ through your favorite browser

这篇关于使用 NodeJS 创建 YouTube 播放列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆