Yaron U.
Yaron U.

Reputation: 7891

Set YouTube video title when uploading with Node.js (googleapi module v1.0)

I'm trying to upload video to my YouTube channel using the googleapi module in Node.js (YouTube API V3)

The video is uploaded fine - I just can't find how to pass title and description to the upload command.

This is my code:

//Authorization stuff above

fs.readFile('./youtube_videos/in.avi', function(err, content){
    if(err){
        console.log('read file error: '+err);
    } else {
        yt.videos.insert({
            part: 'status,snippet',
            autoLevels: true,
            media: {
                body: content
            }
        }, function(error, data){
            if(error){
                console.log('error: '+error);
            } else {
                console.log('https://www.youtube.com/watch?v='+data.id+"\r\n\r\n");
                console.log(data);
            }
        });
    }
})

I know how should pass some snippet object like

snippet: {
    title: 'test upload2',
    description: 'My description2',
}

but I can't find where should it be - I tried every (almost) combination possible

Thank You!

Upvotes: 1

Views: 1191

Answers (1)

Yaron U.
Yaron U.

Reputation: 7891

I found the answer In case someone is looking for it - The snippet should be part of a resource object in the options of the request

(I also converted the fs.readFile to fs.createReadStream)

function uploadToYoutube(video_file, title, description,tokens, callback){
    var google = require("googleapis"),
        yt = google.youtube('v3');

    var oauth2Client = new google.auth.OAuth2(clientId, appSecret, redirectUrl);
    oauth2Client.setCredentials(tokens);
    google.options({auth: oauth2Client});

    return yt.videos.insert({
        part: 'status,snippet',
        resource: {
            snippet: {
                title: title,
                description: description
            },
            status: { 
                privacyStatus: 'private' //if you want the video to be private
            }
        },
        media: {
            body: fs.createReadStream(video_file)
        }
    }, function(error, data){
        if(error){
            callback(error, null);
        } else {
            callback(null, data.id);
        }
    });
};

Upvotes: 1

Related Questions