使用Node.js上传YouTube视频时持续出现Login Required错误求助
尝试用Node.js向YouTube上传视频时,持续遇到**“需要登录(Login Required)”**错误。已确认access token正确(可在控制台打印),且重新生成过token,但问题依旧。以下是完整上传代码:
exports.acceptSubmit = async (req, res) => { try { const notificationId = req.params.notificationId; // Find the notification by ID const notification = await Notification.findById(notificationId); if (!notification) { return res.status(404).json({ message: "Notification not found." }); } // Get the creator's user ID from the notification const creatorId = notification.creatorId; // Find the creator based on creatorId const creator = await User.findById(creatorId); if (!creator) { return res.status(404).json({ message: "Creator not found." }); } // Get the creator's access token console.log(creator); const accessToken = creator.accessToken; const pendingVideo = await PendingVideo.findById(notification.videoId); if (!pendingVideo) { return res.status(404).json({ message: "Pending video not found." }); } const videoTitle = pendingVideo.title; const videoDescription = pendingVideo.description; const videoUrl = pendingVideo.videoUrl; // Assuming this is the S3 URL const thumbnailUrl = pendingVideo.thumbnailUrl; // Assuming this is the S3 URL for thumbnail // authenticate with the YouTube Data API using the creator's access token const youtube = google.youtube({ version: 'v3', auth: accessToken, }); const videoResponse = await youtube.videos.insert({ part: 'snippet,status', requestBody: { snippet: { title: videoTitle, description: videoDescription, }, status: { privacyStatus: 'public', // or 'private' or 'unlisted' }, }, media: { mimeType: 'video/*', body: s3.getObject({ Bucket: 'editingfiles', Key: 'videos/1693044658221-Course_selling_done.mp4', }).createReadStream(), }, }); // get the uploaded video's ID const videoId = videoResponse.data.id; // upload the thumbnail const thumbnailResponse = await youtube.thumbnails.set({ videoId: videoId, media: { mimeType: 'image/jpeg', // or 'image/png' body: s3.getObject({ Bucket: 'editingfiles', Key: 'thumbnails/1693044658221-server code.png', }).createReadStream(), }, }); return res.json("all good"); } catch (error) { console.error(error); res.status(500).json({ message: "Server error." }); } };
错误信息:GaxiosError: 需要登录(Login Required)
Token权限范围不足
YouTube视频上传需要特定OAuth2权限,当前access token可能未包含https://www.googleapis.com/auth/youtube.upload或https://www.googleapis.com/auth/youtube权限。检查生成token的授权流程,确保请求了正确的权限范围。Token已过期或无效
即使能打印token,也可能存在过期(Google OAuth2 access token默认有效期1小时)、或token与请求用户不匹配的情况。可直接调用Google的token验证接口,确认token的有效性、权限范围和过期时间。API客户端配置问题
确认Google Cloud项目已启用YouTube Data API v3,且OAuth2客户端ID类型(Web应用/桌面应用等)与授权流程匹配。服务器端应用需确保使用正确的服务账号或Web应用客户端配置。Token传递方式验证
可尝试在请求头中手动添加Authorization头,替代客户端初始化时的auth参数,验证是否为传递问题:const youtube = google.youtube({ version: 'v3', }); const videoResponse = await youtube.videos.insert({ part: 'snippet,status', requestBody: { /* ... */ }, media: { /* ... */ }, headers: { 'Authorization': `Bearer ${accessToken}` } });S3流间接影响
若S3读取流异常,可能导致请求中断,被API误判为权限问题。可先用本地文件流替代S3流测试,排除文件读取问题。
内容的提问来源于stack exchange,提问作者Saim Sheikh

