Search code examples
node.jsyoutubeyoutube-apigoogle-api-nodejs-client

How can I post a comment on a YouTube video? (Node.js)


I want to post a comment on a YouTube video programmatically using google-api-nodejs-client. Any ideas how can I do so?

I would really appreciate if a code sample is provided.

Thanks!


Solution

  • I've managed to do it!

    Here's the code in case anyone is looking for an answer.

    var oauth2Client = new OAuth2("CLIENT_ID", "CLIENT_SECRET", []),
        google = require('googleapis'),
        ytdapi = google.youtube('v3');
    
    oauth2Client.setCredentials({
      refresh_token: "REFRESH_TOKEN"
    });
    
    var params = {
        auth: oauth2Client,
        part: "snippet",
        resource: {
          snippet: {
            channelId: "CHANNEL_ID",
            videoId: "VIDEO_ID",
          topLevelComment: {
            snippet: {
              textOriginal: "YOUR TEXT HERE"
            }
          }
        }
      }
    };
    
    
    ytdapi.commentThreads.insert(params, function(err, data) {
      if (err) {
        console.log(err);
        res.status(400).send("Error posting comment ");
      }
      else {
        console.log(data);
        res.status(200).send("Successfully posted comment ");
      }
    });