Search code examples
node.jsgoogle-apiyoutube-data-apiservice-accounts

YouTube account authentication nodejs


I am making an application that searches for YouTube videos, and creates a playlist with those videos.

I've figured how to search for videos just using the API key provided, but am completely lost on how to do stuff as a user. At first, I thought that I could just use a service account to host all of the playlist, but I've learned quickly that YouTube doesn't support that.

My ultimate goal is to just create a playlist on my personal YouTube account, so I want that account to be hardcoded in.

I am struggling on changing it from using a service account to my personal account. Many other solutions I've seen use an express server with a frontend component, neither of which I have.

  import { google } from 'googleapis';
  import fs from 'fs';

  const config = JSON.parse(fs.readFileSync('config.json', 'utf8'));

  const key = require('./credentials.json');

  const scopes = "https://www.googleapis.com/auth/youtube";

  const jwt = new google.auth.JWT(key.client_email, null, key.private_key, scopes);

  jwt.authorize((err, response) => {

     const yt = google.youtube({
        version: 'v3',
        auth: jwt,
     });

     yt.search.list({
        part: 'snippet',
        q: 'study music | zen music -livestream -live',
        maxResults: 1,
     }, (err, data) => {

        if (err) console.error(err);

        if (data) {

           console.log(data.data.items);
           // TODO: create playlist with these items

        }

     });

  });

Currently, it's using a service account to search for videos. I haven't done the playlist creation part yet because it would just keep giving an error as I'm using a service account.


Solution

  • The YouTube API does not support service accounts. You will need to use Oauth2 and save the refresh token. You can then use your refresh token to request a new access token whenever you need.

    I recommend following quickstart

    var fs = require('fs');
    var readline = require('readline');
    var {google} = require('googleapis');
    var OAuth2 = google.auth.OAuth2;
    
    // If modifying these scopes, delete your previously saved credentials
    // at ~/.credentials/youtube-nodejs-quickstart.json
    var SCOPES = ['https://www.googleapis.com/auth/youtube.readonly'];
    var TOKEN_DIR = (process.env.HOME || process.env.HOMEPATH ||
        process.env.USERPROFILE) + '/.credentials/';
    var TOKEN_PATH = TOKEN_DIR + 'youtube-nodejs-quickstart.json';
    
    // Load client secrets from a local file.
    fs.readFile('client_secret.json', function processClientSecrets(err, content) {
      if (err) {
        console.log('Error loading client secret file: ' + err);
        return;
      }
      // Authorize a client with the loaded credentials, then call the YouTube API.
      authorize(JSON.parse(content), getChannel);
    });
    
    /**
     * Create an OAuth2 client with the given credentials, and then execute the
     * given callback function.
     *
     * @param {Object} credentials The authorization client credentials.
     * @param {function} callback The callback to call with the authorized client.
     */
    function authorize(credentials, callback) {
      var clientSecret = credentials.installed.client_secret;
      var clientId = credentials.installed.client_id;
      var redirectUrl = credentials.installed.redirect_uris[0];
      var oauth2Client = new OAuth2(clientId, clientSecret, redirectUrl);
    
      // Check if we have previously stored a token.
      fs.readFile(TOKEN_PATH, function(err, token) {
        if (err) {
          getNewToken(oauth2Client, callback);
        } else {
          oauth2Client.credentials = JSON.parse(token);
          callback(oauth2Client);
        }
      });
    }
    
    /**
     * Get and store new token after prompting for user authorization, and then
     * execute the given callback with the authorized OAuth2 client.
     *
     * @param {google.auth.OAuth2} oauth2Client The OAuth2 client to get token for.
     * @param {getEventsCallback} callback The callback to call with the authorized
     *     client.
     */
    function getNewToken(oauth2Client, callback) {
      var authUrl = oauth2Client.generateAuthUrl({
        access_type: 'offline',
        scope: SCOPES
      });
      console.log('Authorize this app by visiting this url: ', authUrl);
      var rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout
      });
      rl.question('Enter the code from that page here: ', function(code) {
        rl.close();
        oauth2Client.getToken(code, function(err, token) {
          if (err) {
            console.log('Error while trying to retrieve access token', err);
            return;
          }
          oauth2Client.credentials = token;
          storeToken(token);
          callback(oauth2Client);
        });
      });
    }
    
    /**
     * Store token to disk be used in later program executions.
     *
     * @param {Object} token The token to store to disk.
     */
    function storeToken(token) {
      try {
        fs.mkdirSync(TOKEN_DIR);
      } catch (err) {
        if (err.code != 'EEXIST') {
          throw err;
        }
      }
      fs.writeFile(TOKEN_PATH, JSON.stringify(token), (err) => {
        if (err) throw err;
        console.log('Token stored to ' + TOKEN_PATH);
      });
      console.log('Token stored to ' + TOKEN_PATH);
    }
    
    /**
     * Lists the names and IDs of up to 10 files.
     *
     * @param {google.auth.OAuth2} auth An authorized OAuth2 client.
     */
    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.data.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);
        }
      });
    }