首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用Google脚本将视频上传到YouTube?

如何使用Google脚本将视频上传到YouTube?
EN

Stack Overflow用户
提问于 2020-07-31 08:09:11
回答 1查看 925关注 0票数 0

我想上传一个视频到YouTube使用Google脚本。这是我的代码,包括我尝试过的内容和错误。

Code.gs

代码语言:javascript
运行
复制
/**
 * @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资源,并授权脚本运行。

我做错了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-31 08:22:51

这个修改怎么样?

修改要点:

  • 请将status包含在videoResource中。
  • YouTube.Videos.insert(resource, part, mediaData)的第二个论点是part。在这种情况下,它是"snippet,status"

修改脚本:

代码语言:javascript
运行
复制
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

注意:

  • 在这种情况下,需要在高级谷歌服务中启用YouTube数据API v3。请小心这个。参考

参考文献:

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63188106

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档