问题
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);
}
});
}
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
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');
}
});
}
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?
回答1:
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.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);
}
});
});
});
After you run your script, just go to http://localhost:5000/ through your favorite browser
回答2:
For nodejs,
I suggest you to use these nodeJS modules developed by Google.
npm install googleapis --save
npm install google-auth-library --save
- googleapis
- 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;
...
}
来源:https://stackoverflow.com/questions/45554160/create-youtube-playlist-using-nodejs