我想上传一个视频到YouTube使用Google脚本。这是我的代码,包括我尝试过的内容和错误。
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>, )
正如您从代码中看到的那样,我已经多次尝试使用YouTube.Videos.insert()
方法。但是每次尝试都会抛出一个错误。是的,我已经在“资源”选项卡下启用了YouTube Data v3资源,并授权脚本运行。
我做错了什么?
发布于 2020-07-31 08:22:51
这个修改怎么样?
修改要点:
status
包含在videoResource
中。YouTube.Videos.insert(resource, part, mediaData)
的第二个论点是part
。在这种情况下,它是"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
注意:
参考文献:
https://stackoverflow.com/questions/63188106
复制相似问题