Im trying to upload some videos to some Youtube channels i own, using pure nodejs code, i follow all steps from the API and examples i found, but for some reason the video details is uploaded, but the media (the video itself) is not, the video keep "processing" forever without miniature of the video, and the upload process of a 15Mb video take only 2 seconds, like the media is not correctly uploaded for some reason.
Im using a local auth stored in files for different channels, so first I use the auth function and then the upload one:
const {google} = require('googleapis')const {authenticate} = require('@google-cloud/local-auth')const {OAuth2Client} = require('google-auth-library')const youtube = google.youtube('v3') async authYoutubeAPI() { /* * this use the oauth2.keys.json file (from Google Dev console Oauth Credentials) * every channel is selected when you login into the Google Account, * and when you login you select the channel and token.json is created */ let auth = null if (fs.existsSync(this.configs.youtube_api_token_file)) { console.log('authYoutubeAPI using token '+ this.configs.youtube_api_token_file) // If the token file exists, load the token from it const token = fs.readFileSync(this.configs.youtube_api_token_file, 'utf-8'); const credentials = JSON.parse(token); // Create an OAuth2 client with the stored credentials const oauth2Client = new OAuth2Client(this.configs.youtube_api_id, this.configs.youtube_api_secret); oauth2Client.setCredentials(credentials); auth = oauth2Client } else { console.log('authYoutubeAPI requesting new token..') // Obtain user credentials to use for the request auth = await authenticate({ keyfilePath: path.join(__dirname, './oauth2.keys.json'), scopes: ['https://www.googleapis.com/auth/youtube.upload','https://www.googleapis.com/auth/youtube', ], }); fs.writeFileSync(this.configs.youtube_api_token_file, JSON.stringify(auth.credentials)); } google.options({auth}); return auth; } async uploadVideoAPI(auth, requestBody, videoFile) { const fileSize = fs.statSync(videoFile).size; const res = await youtube.videos.insert( { part: 'id,snippet,status', requestBody: requestBody, media: { //mimeType: 'video/mp4', // this dont help body: fs.createReadStream(videoFile), }, } ) // Check if the file exists if (!fs.existsSync(videoFile)) { console.error('Video file not found.'); return; } // Check file size (optional) const stats = fs.statSync(videoFile); if (stats.size === 0) { console.error('Video file is empty.'); return; } console.log('\n\n') console.log(res.data) return res.data } let auth = await this.authYoutubeAPI() let requestBody = { snippet: { title: videoData.title, description: videoData.description, defaultLanguage: 'en', audioLanguage: 'en', tags: videoData.tags, notifySubscribers: true }, status: { privacyStatus: 'private', } } console.log(filePath) await this.uploadVideoAPI(auth, requestBody, filePath)The auth works because i can upload and see in Youtube the video, butlike i say, the media, the video itself is like is not there, but theAPI reply me correctly and i see no messages that the video file ismissing or somplething else.
{"kind": "youtube#video","etag": "xxxrXCMnD5NwQ7Qc_mSgvrtbM","id": "xxx","snippet": {"publishedAt": "2024-03-01T10:00:04Z","channelId": "xxx","title": "xxxxxxxxxxxx","description": "xxxxxxxxxxxx","thumbnails": {"default": {"url": "https://i9.ytimg.com/vi/xxx/default.jpg?sqp=CKDOhq8G&rs=AOn4CLA2b_opJg4N5m8Q5F4siyTiYGNRWQ","width": 120,"height": 90 },"medium": {"url": "https://i9.ytimg.com/vi/xxx/mqdefault.jpg?sqp=CKDOhq8G&rs=AOn4CLBfDKTRVOgfHJ-u4wN9HYQcW8LHcg","width": 320,"height": 180 },"high": {"url": "https://i9.ytimg.com/vi/xxx/hqdefault.jpg?sqp=CKDOhq8G&rs=AOn4CLB2Z4kwtWbuGs210PlONmE0pr0JoA","width": 480,"height": 360 } },"channelTitle": "xxx","tags": ["xxx","xxx" ],"categoryId": "22","liveBroadcastContent": "none","defaultLanguage": "en","localized": {"title": "xxxxxxxxxxxx","description": "xxxxxxxxxxxx" } },"status": {"uploadStatus": "uploaded","privacyStatus": "private","license": "youtube","embeddable": true,"publicStatsViewable": true }}