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

编程入门 行业动态 更新时间:2024-10-27 03:42:17
本文介绍了如何使用 Google Apps 脚本将视频上传到 Youtube?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想使用 Google Apps 脚本通过 YouTube 数据 API v3 上传视频 YouTube.这是我的代码:

I want to use Google Apps Script to upload a video YouTube using the YouTube Data API v3. This is my code:

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 Data API.

The response is "Unauthorized", I check the Google Console is enable, and in the Script also enable the Youtube Data API.

推荐答案

如果您的 Google Drive 中有一个小于 50MB 的 MP4 文件,那么您可以从 Google Drive 中获取该文件并使用YouTube 数据 API.我创建了一个新的 Google Cloud Platform (GCP) 项目,启用了 YouTube Data API 和 Google Drive API,并将新的 GCP 项目与我的 Apps Script 项目相关联.另外,我手动将所需的范围添加到了 appsscript.json 文件中.

If you have an MP4 file in your Google Drive that is less than 50MB, then you can get the file from your Google Drive and upload it to YouTube using the YouTube Data API. I created a new Google Cloud Platform (GCP) project, and enabled the YouTube Data API and the Google Drive API, and associated the new GCP project with my Apps Script project. Also, I manually added the needed scopes to the appsscript.json file.

{ "timeZone": "Your time zone will be here", "dependencies": { "enabledAdvancedServices": [ { "userSymbol": "YouTube", "version": "v3", "serviceId": "youtube" } ] }, "oauthScopes": ["www.googleapis/auth/drive", "www.googleapis/auth/script.external_request","www.googleapis/auth/youtube"], "exceptionLogging": "STACKDRIVER", "runtimeVersion": "V8" }

代码:

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: drive.google/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 = `www.googleapis/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); }

更多推荐

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

本文发布于:2023-11-17 07:41:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1609336.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   脚本   视频   Apps   Google

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!