Unauthorized Error Using YouTube Data API with Service Account in Node.js

I am attempting to upload videos to YouTube using the YouTube Data API and a service account in my Node.js backend. However, I keep encountering an “Unauthorized” error. Here is a summary of what I have done so far:

Set Up Service Account:

Created a service account in the Google Cloud Console and downloaded the JSON key file.
Enabled domain-wide delegation and provided the necessary permissions in the G Suite Admin console.
Granted the following scopes: https://www.googleapis.com/auth/youtube.upload.
Backend Code:

const multer = require('multer');
const path = require('path');
const fs = require('fs');
const { google } = require('googleapis');
const credentials = require('./path/to/your/service-account-file.json');

const router = express.Router();

const scopes = ['https://www.googleapis.com/auth/youtube.upload'];

// Initialize the JWT client
const auth = new google.auth.JWT(
  credentials.client_email,
  null,
  credentials.private_key,
  scopes,
  '[email protected]' // G Suite user email
);

// Function to upload a video
async function uploadVideo(req, res) {
  const filePath = req.file.path;
  const { fileName } = req.body;
  try {
    const youtube = google.youtube({
      version: 'v3',
      auth,
    });

    if (!filePath) {
      return res.status(400).json({ error: 'File data not provided' });
    }

    // Video resource
    const videoParams = {
      part: 'snippet,status',
      requestBody: {
        snippet: {
          title: req.body.title || 'Test',
          description: '',
          tags: [],
        },
        status: {
          privacyStatus: 'public',
        },
      },
      media: {
        body: fs.createReadStream(filePath),
      },
    };

    // Execute the request to upload the video
    const response = await youtube.videos.insert(videoParams, {
      onUploadProgress: (evt) => {
        const progress = (evt.bytesRead / videoParams.media.body._readableState.length) * 100;
        console.log(`${Math.round(progress)}% uploaded`);
      },
    });

    console.log('Video uploaded:', response.data);
    const videoUrl = `https://www.youtube.com/watch?v=${response.data.id}`;

    // Send response with video URL
    res.status(200).json({ message: 'Video uploaded successfully', videoUrl });
  } catch (error) {
    console.error('Error uploading video:', error);
    res.status(500).json({ error: 'Failed to upload video', message: error.message });
  }
}

const storage = multer.diskStorage({
  destination: 'uploads',
  filename: function (req, file, callback) {
    callback(null, `${Date.now()}.mp4`);
  },
});

const upload = multer({
  storage,
  limits: { fileSize: 100 * 1024 * 1024 },
  fileFilter: function (req, file, cb) {
    const { mimetype } = file;
    if (mimetype.startsWith('video/')) {
      cb(null, true);
    } else {
      cb(new Error('Invalid file type'));
    }
  },
});

router.post('/youtube-upload', upload.single('file'), uploadVideo);

module.exports = router;
Frontend Code:

javascript
Copy code
const handleClick = async (e) => {
  e.preventDefault();
  const file = e.target.files[0];
  const formData = new FormData();
  formData.append('file', file);
  formData.append('fileName', file.name);

  if (file.type.startsWith('video/')) {
    try {
      const response = await fetch('/youtube-upload', {
        method: 'POST',
        body: formData,
      });

      if (!response.ok) {
        throw new Error('Failed to upload video to YouTube');
      }

      const data = await response.json();
      console.log('YouTube upload successful', data);

      // Update state with the video URL or handle success
    } catch (error) {
      console.error('Error uploading video to YouTube:', error.message);
      // Handle error (e.g., show error message to user)
    }
  } else {
    // Handle non-video uploads
  }
}; ```
Despite these steps, I am getting the following error message:


GaxiosError: Unauthorized
I have ensured that:

The service account has the appropriate permissions.
The JWT token is correctly generated and used.
What am I missing or doing wrong? Any help would be greatly appreciated.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật