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

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

Stack Overflow用户
提问于 2016-12-06 11:05:20
回答 2查看 1.3K关注 0票数 4

我想使用谷歌应用程序脚本上传一个使用YouTube数据接口v3的视频YouTube。这是我的代码:

代码语言:javascript
运行
复制
function YouTubeAPI()
{
  var url = {URL VIDEO};
  var file = UrlFetchApp.fetch(url).getBlob();
  Logger.log(file.getName());
  var snippet = {
  "snippet": {
    "title": "Summer vacation in California",
    "description": "Had a great time surfing in Santa Cruz",
    "tags": ["surfing", "Santa Cruz"],
    "categoryId": "22"},"status": {"privacyStatus": "private"}};
  YouTube.Videos.insert(snippet, 'snippet,status', file)
}

响应是“未授权的”,我检查Google控制台是否启用,并且在脚本中还启用了Youtube数据API。

EN

回答 2

Stack Overflow用户

发布于 2021-04-20 00:13:57

如果你的Google Drive中有一个小于50MB的MP4文件,那么你可以从Google Drive中获取该文件,并使用YouTube Data API将其上传到YouTube。我创建了一个新的Google Cloud Platform ( GCP )项目,启用了YouTube Data API和Google Drive API,并将这个新的GCP项目与我的Apps Script项目关联起来。另外,我手动将所需的作用域添加到appsscript.json文件中。

代码语言:javascript
运行
复制
{
  "timeZone": "Your time zone will be here",
  "dependencies": {
    "enabledAdvancedServices": [
      {
        "userSymbol": "YouTube",
        "version": "v3",
        "serviceId": "youtube"
      }
    ]
  },
  "oauthScopes": ["https://www.googleapis.com/auth/drive", "https://www.googleapis.com/auth/script.external_request","https://www.googleapis.com/auth/youtube"],
  "exceptionLogging": "STACKDRIVER",
  "runtimeVersion": "V8"
}

代码:

代码语言:javascript
运行
复制
function uploadToYouTube(mp4_fileId) {
  var blob,mp4_fileId,part,requestResource,response;
  var options = {},snippet = {};

  /*
    You will need to create a GCP standard project and associate it with this Apps Script project-
    In the new code editor click the settings cog wheel and scroll down to:
    Google Cloud Platform (GCP) Project -
    You may get an error:
    In order to change your project, you will need to configure the OAuth consent screen. Configure your OAuth Consent details.
    
    And if you do not have a Google Workspace account then you wont be able to set up the GCP project as "INTERNAL"
    You will need to enable the Google Drive API and the YouTube API in the associated GCP project -
  */

  /*
    This code needs the file ID of the MP4 file in your Google Drive - 
    To get the file ID of an MP4 video file in Google Drive, right click the MP4 in your Google Drive
    and choose, "Get link"
    The link will look like this:
    https://drive.google.com/file/d/FILE_ID_IS_HERE/view?usp=sharing
    In the URL is the file ID
  */

  options = {
    "method" : "get",
    "headers" : {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
    "muteHttpExceptions":true
  }
  
  mp4_fileId = mp4_fileId ? mp4_fileId : 'PUT_YOUR_MP4_FILE_ID_HERE';

  const url = `https://www.googleapis.com/drive/v3/files/` + mp4_fileId + `?alt=media`;
  response = UrlFetchApp.fetch(url, options);
  //Logger.log('response.getResponseCode(): ' + response.getResponseCode())

  if (response.getResponseCode() !== 200) {

    return;
  }
  
  blob = response.getBlob();
  //Logger.log('blob.getName(): ' + blob.getName())

  /*
  {"snippet":{
    "playlistId":"YOUR_PLAYLIST_ID",
    "position":0,
    "resourceId":{
      "kind":"youtube#video",
      "videoId":"abcdefg"
      }
    }
  }
  */

  /*
  {
  "snippet": {
    "title": "Summer vacation in California",
    "description": "Had fun surfing in Santa Cruz",
    "tags": ["surfing", "Santa Cruz"],
    "categoryId": "22"
  },
  "status": {
    "privacyStatus": "private"
  }
  }
  */
  
  requestResource = {};

  snippet.title = "AAA_Put_Title_Here";
  snippet.description = "Description of video goes here";
  snippet.categoryId = "22";

  options.snippet = snippet;
  options.status = {
      "privacyStatus": "private"
    }

  part = "snippet,status";//This correlates to the options

  //YouTube.Videos.insert(resource: Youtube_v3.Youtube.V3.Schema.Video, part: string[], mediaData: Blob, optionalArgs: Object)
  var response = YouTube.Videos.insert(requestResource, part, blob, options);

  if (!response || !response.kind) {//There was an error
    console.log("Error!")
  }
  
  //Logger.log('response: ' + response);

}
票数 1
EN

Stack Overflow用户

发布于 2016-12-07 17:10:32

尝试使用code below。此示例代码查找用户的上传内容,然后通过附加一个字符串来更新最近上传内容的描述。

代码语言:javascript
运行
复制
/**
 * This sample finds the active user's uploads, then updates the most recent
 * upload's description by appending a string.
 */
function updateVideo() {
  // 1. Fetch all the channels owned by active user
  var myChannels = YouTube.Channels.list('contentDetails', {mine: true});
  // 2. Iterate through the channels and get the uploads playlist ID
  for (var i = 0; i < myChannels.items.length; i++) {
    var item = myChannels.items[i];
    var uploadsPlaylistId = item.contentDetails.relatedPlaylists.uploads;

    var playlistResponse = YouTube.PlaylistItems.list('snippet', {
      playlistId: uploadsPlaylistId,
      maxResults: 1
    });

    // Get the videoID of the first video in the list
    var video = playlistResponse.items[0];
    var originalDescription = video.snippet.description;
    var updatedDescription = originalDescription + ' Description updated via Google Apps Script';

    video.snippet.description = updatedDescription;

    var resource = {
      snippet: {
        title: video.snippet.title,
        description: updatedDescription,
        categoryId: '22'
      },
      id: video.snippet.resourceId.videoId
    };
    YouTube.Videos.update(resource, 'id,snippet');
  }
}

有关更多信息,请在此处下载演示应用程序:https://github.com/youtube/api-samples

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

https://stackoverflow.com/questions/40987147

复制
相关文章

相似问题

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