【问题标题】:How to upload a video to YouTube using Google Apps Script?如何使用 Google Apps 脚本将视频上传到 YouTube?
【发布时间】:2020-11-21 01:55:48
【问题描述】:

我想使用 Google Apps 脚本将视频上传到 YouTube。这是我的代码,包括我尝试过的内容以及错误所在。

代码.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 数据 API v3 资源并授权脚本运行。

我做错了什么?

【问题讨论】:

    标签: javascript api google-apps-script video youtube


    【解决方案1】:

    这个修改怎么样?

    修改点:

    • 请在videoResource 中包含status
    • 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
    

    注意:

    • 在这种情况下,需要在高级 Google 服务中启用 YouTube Data API v3。请注意这一点。 Ref

    参考资料:

    【讨论】:

      猜你喜欢
      • 2017-04-20
      • 2014-04-06
      • 2021-05-22
      • 1970-01-01
      • 2021-06-30
      • 2019-09-24
      • 2015-04-23
      • 1970-01-01
      相关资源
      最近更新 更多