我無法測驗我的腳本。如果您可以幫助此處的代碼 - 它不起作用根據檔案 - https://developers.google.com/youtube/v3/docs/videos/insert
if (serviceYT.hasAccess()) {
url = 'https://youtube.googleapis.com/youtube/v3/videos?part=snippet,contentDetails,statistics&mine=true&key=' API_KEY;
var data;
data = '{"snippet":{"title":"testtitle","description":"testdes","categoryId":"19","tags":["asdf","sadfds"]},"status":{"privacyStatus":"public"}}';
var options = {
'headers': {
'Authorization': 'Bearer ' serviceYT.getAccessToken()
,'Accept': 'application/json'
},
'contentType': 'application/json',
'method' : 'POST',
'payload' : data,
'muteHttpExceptions' : true
};
//execute and handle the response
var response = UrlFetchApp.fetch(url, options);
var responseCode = response.getResponseCode();
var result = JSON.parse(response.getContentText());
Logger.log(result);
}
我的問題——
- 把視頻放在哪里?
- 我得到的錯誤的解決方案:
{error={message='status', code=400.0, errors=[{reason=unexpectedPart, domain=youtube.part, message='status', location=part, locationType=parameter}]}}
一個 youtube 物件(基本上是一個空視頻)已成功添加到通過作業室可見的 youtube。它是一個空視頻,但標題、描述、狀態等......其他內容都是正確設定。
關于如何添加媒體或視頻的任何想法?必須在有效負載中的何處添加視頻 blob?
我無法在檔案中獲得它。 https://developers.google.com/youtube/v3/docs/videos/insert
uj5u.com熱心網友回復:
我相信你的目標如下。
- 您想將帶有標題、說明等的電影檔案上傳到您的 YouTube 頻道。
- 電影檔案放在您的 Google 云端硬碟中。
- 您希望通過使用
UrlFetchApp.
在這種情況下,以下修改后的腳本如何?
修改后的腳本:
在使用此腳本之前,請啟用 YouTube Data API v3 并添加https://www.googleapis.com/auth/youtube. 并將您的 Google Drive 中電影檔案的檔案 ID 設定為const fileId = "###";.
serviceYT.getAccessToken() 來自你的腳本。
function myFunction() {
const fileId = "###"; // Please set the file ID of movie file on the Google Drive.
const metadata = {
"snippet": { "title": "testtitle", "description": "testdes", "categoryId": "19", "tags": ["asdf", "sadfds"] },
"status": { "privacyStatus": "public" }
};
const url = 'https://www.googleapis.com/upload/youtube/v3/videos?part=snippet,status';
const file = DriveApp.getFileById(fileId);
const boundary = "xxxxxxxxxx";
let data = "--" boundary "\r\n";
data = "Content-Type: application/json; charset=UTF-8\r\n\r\n";
data = JSON.stringify(metadata) "\r\n";
data = "--" boundary "\r\n";
data = "Content-Type: " file.getMimeType() "\r\n\r\n";
const payload = Utilities.newBlob(data).getBytes().concat(file.getBlob().getBytes()).concat(Utilities.newBlob("\r\n--" boundary "--").getBytes());
const options = {
method: "post",
contentType: "multipart/form-data; boundary=" boundary,
payload: payload,
headers: { 'Authorization': 'Bearer ' serviceYT.getAccessToken() },
muteHttpExceptions: true,
};
const res = UrlFetchApp.fetch(url, options).getContentText();
console.log(res);
}
在您的腳本中,您使用的是
url = 'https://youtube.googleapis.com/youtube/v3/videos?part=snippet,contentDetails,statistics&mine=true&key=' API_KEY;. 你data擁有的財產snippet和status。在這種情況下,需要使用 的端點https://www.googleapis.com/upload/youtube/v3/videos?part=snippet,status。為了上傳電影檔案,需要使用
multipart/form-data.運行此腳本時,Google Drive 上的電影檔案將上傳到 YouTube。
筆記:
在現階段,即使
"privacyStatus": "public"使用,上傳的視頻也是不公開的。關于這個,可以在官方檔案中看到如下。參考從 2020 年 7 月 28 日之后創建的未經驗證的 API 專案通過videos.insert 端點上傳的所有視頻將被限制為私人觀看模式。要解除此限制,每個 API 專案都必須經過審核以驗證是否符合服務條款。有關更多詳細資訊,請參閱 API 修訂歷史記錄。
當您在高級 Google 服務中使用 YouTube API 時,您可以使用以下簡單腳本。參考
function myFunction2() { const fileId = "###"; // Please set the file ID of movie file on the Google Drive. const res = YouTube.Videos.insert({ "snippet": { "title": "testtitle", "description": "testdes", "categoryId": "19", "tags": ["asdf", "sadfds"] }, "status": { "privacyStatus": "public" } }, ["snippet", "status"], DriveApp.getFileById(fileId).getBlob()); console.log(res); }
參考:
- 視頻:插入
- 使用 Google Apps 腳本的 Multipart-POST 請求
- 獲取(網址,引數)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/344406.html
