Reputation: 16122
I want to upload a video to YouTube using Google Apps Script. Here is my code, including what I've tried and what the errors are.
Code.gs/**
* @see https://developers.google.com/youtube/v3/docs/videos/insert
* @param { String } sourceUrl url location of the source video file in google drive
* @returns { Video }
*/
const uploadVideo2youtube = sourceId => {
const sourceFile = DriveApp.getFileById( sourceId, ).getBlob();
const videoResource = {
snippet: {
title: 'Summer vacation in California',
description: 'Had a great time surfing in Santa Cruz',
tags: [ 'surfing', 'Santa Cruz', ],
categoryId: '22',
},
};
// const status = { privacyStatus: 'Private', };
const status = { privacyStatus: 'private', };
// here are all my attempts and the errors with each one...
// const newVideo = YouTube.Videos.insert( videoResource, status, sourceFile, ); // GoogleJsonResponseException: API call to youtube.videos.insert failed with error: '{privacyStatus=private}' (line 229, file "Code")
// const newVideo = YouTube.Videos.insert( videoResource, sourceFile, ); // GoogleJsonResponseException: API call to youtube.videos.insert failed with error: Blob (line 230, file "Code")
const newVideo = YouTube.Videos.insert( videoResource, [ status ], sourceFile, ); // GoogleJsonResponseException: API call to youtube.videos.insert failed with error: '{privacyStatus=private}' (line 231, file "Code")
Logger.log('(line 213) newVideo:\n%s', JSON.stringify( newVideo ),);
}
const upload2youtube_test = () => upload2youtube( <fileId>, )
As you can see from the code, I have made several attempts at the YouTube.Videos.insert()
method. But each attempt throws an error. Yes, I have enabled the YouTube Data API v3 resource under my resources tab and authorized the script to run.
What am I doing wrong?
Upvotes: 0
Views: 1378
Reputation: 201513
How about this modification?
status
in videoResource
.YouTube.Videos.insert(resource, part, mediaData)
is part
. In this case, it's "snippet,status"
.const sourceFile = DriveApp.getFileById(sourceId).getBlob();
const videoResource = {
snippet: {
title: 'Summer vacation in California',
description: 'Had a great time surfing in Santa Cruz',
tags: [ 'surfing', 'Santa Cruz', ],
categoryId: '22',
},
status: {privacyStatus: 'private'} // Added
};
const newVideo = YouTube.Videos.insert( videoResource, "snippet,status", sourceFile); // Modified
Upvotes: 3