YouTube
透過集合功能整理內容 你可以依據偏好儲存及分類內容。
更新影片的詳細資料
function updateYouTubeVideo() { // 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 ID 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'); console.log('Video with ID = %s and Title = %s was successfully updated.', video.snippet.resourceId.videoId, video.snippet.title); } }
建立頻道公佈欄
function postChannelBulletin() { var message = 'Thanks for subscribing to my channel! This posting is ' + 'from Google Apps Script'; var videoId = 'INSERT_VIDEO_ID_HERE'; var resource = { snippet: { description: message }, contentDetails: { bulletin: { resourceId: { kind: 'youtube#video', videoId: videoId } } } }; var response = YouTube.Activities.insert(resource, 'snippet,contentDetails'); console.log('Posted to channel bulletin successfully.'); }
擷取影片上傳項目
function retrieveVideoUploads() { var results = YouTube.Channels.list('contentDetails', {mine: true}); for (var i in results.items) { var item = results.items[i]; // Get the playlist ID, which is nested in contentDetails, as described in // the Channel resource: // https://developers.google.com/youtube/v3/docs/channels var playlistId = item.contentDetails.relatedPlaylists.uploads; var nextPageToken = ''; // This loop retrieves a set of playlist items and checks the nextPageToken // in the response to determine whether the list contains additional items. // It repeats that process until it has retrieved all of the items in the // list. while (nextPageToken != null) { var playlistResponse = YouTube.PlaylistItems.list('snippet', { playlistId: playlistId, maxResults: 25, pageToken: nextPageToken }); for (var j = 0; j < playlistResponse.items.length; j++) { var playlistItem = playlistResponse.items[j]; console.log('[%s] Title: %s', playlistItem.snippet.resourceId.videoId, playlistItem.snippet.title); } nextPageToken = playlistResponse.nextPageToken; } } }
按關鍵字搜尋影片
function searchVideosByKeyword() { var results = YouTube.Search.list('id,snippet', {q: 'dogs', maxResults: 25}); for (var i in results.items) { var item = results.items[i]; console.log('[%s] Title: %s', item.id.videoId, item.snippet.title); } }
按主題搜尋影片
function searchVideosByFreebaseTopic() { // See https://developers.google.com/youtube/v3/guides/searching_by_topic // for more details. // Insert Your Freebase topic ID here. The Freebase ID used in this example // corresponds to the Freebase entry for Google. See // http://www.freebase.com/m/045c7b for more details. var mid = '/m/045c7b'; var results = YouTube.Search.list('id,snippet', {topicId: mid, maxResults: 25}); for (var i in results.items) { var item = results.items[i]; console.log('[%s] Title: %s', item.id.videoId, item.snippet.title); } }
訂閱頻道
function subscribeToChannel() { // Replace this channel ID with the channel ID you want to subscribe to. var channelId = 'INSERT_YOUTUBE_CHANNEL_ID_HERE'; var resource = { snippet: { resourceId: { kind: 'youtube#channel', channelId: channelId } } }; try { var response = YouTube.Subscriptions.insert(resource, 'snippet'); console.log('Subscribed to channel ID %s successfully.', channelId); } catch (e) { if (e.message.match('subscriptionDuplicate')) { console.log('Cannot subscribe; already subscribed to channel: ' + channelId); } else { console.log('Error adding subscription: ' + e.message); } } }
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-21 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-08-21 (世界標準時間)。"],[[["\u003cp\u003eThis script provides functionalities for managing YouTube channels and videos, such as updating video details, posting channel bulletins, and retrieving video uploads.\u003c/p\u003e\n"],["\u003cp\u003eUsers can leverage the provided functions to find videos based on keywords or Freebase topics.\u003c/p\u003e\n"],["\u003cp\u003eThe script allows for subscribing to channels and managing channel subscriptions, handling potential duplicate subscriptions.\u003c/p\u003e\n"],["\u003cp\u003eIt demonstrates the usage of Google Apps Script to interact with the YouTube API for automating channel and video management tasks.\u003c/p\u003e\n"]]],[],null,["# YouTube\n\nUpdate video's details\n----------------------\n\n```gdscript\nfunction updateYouTubeVideo() {\n // 1. Fetch all the channels owned by active user.\n var myChannels = YouTube.Channels.list('contentDetails', {mine: true});\n\n // 2. Iterate through the channels and get the uploads playlist ID.\n for (var i = 0; i \u003c myChannels.items.length; i++) {\n var item = myChannels.items[i];\n var uploadsPlaylistId = item.contentDetails.relatedPlaylists.uploads;\n\n var playlistResponse = YouTube.PlaylistItems.list('snippet', {\n playlistId: uploadsPlaylistId,\n maxResults: 1\n });\n\n // Get the ID of the first video in the list.\n var video = playlistResponse.items[0];\n var originalDescription = video.snippet.description;\n var updatedDescription = originalDescription +\n ' Description updated via Google Apps Script';\n\n video.snippet.description = updatedDescription;\n\n var resource = {\n snippet: {\n title: video.snippet.title,\n description: updatedDescription,\n categoryId: '22'\n },\n id: video.snippet.resourceId.videoId\n };\n YouTube.Videos.update(resource, 'id,snippet');\n\n console.log('Video with ID = %s and Title = %s was successfully updated.',\n video.snippet.resourceId.videoId, video.snippet.title);\n }\n}\n```\n\nCreate a channel bulletin\n-------------------------\n\n```gdscript\nfunction postChannelBulletin() {\n var message = 'Thanks for subscribing to my channel! This posting is ' +\n 'from Google Apps Script';\n\n var videoId = 'INSERT_VIDEO_ID_HERE';\n var resource = {\n snippet: {\n description: message\n },\n contentDetails: {\n bulletin: {\n resourceId: {\n kind: 'youtube#video',\n videoId: videoId\n }\n }\n }\n };\n\n var response = YouTube.Activities.insert(resource, 'snippet,contentDetails');\n console.log('Posted to channel bulletin successfully.');\n}\n```\n\nRetrieve video uploads\n----------------------\n\n```gdscript\nfunction retrieveVideoUploads() {\n var results = YouTube.Channels.list('contentDetails', {mine: true});\n for (var i in results.items) {\n var item = results.items[i];\n // Get the playlist ID, which is nested in contentDetails, as described in\n // the Channel resource:\n // https://developers.google.com/youtube/v3/docs/channels\n var playlistId = item.contentDetails.relatedPlaylists.uploads;\n\n var nextPageToken = '';\n\n // This loop retrieves a set of playlist items and checks the nextPageToken\n // in the response to determine whether the list contains additional items.\n // It repeats that process until it has retrieved all of the items in the\n // list.\n while (nextPageToken != null) {\n var playlistResponse = YouTube.PlaylistItems.list('snippet', {\n playlistId: playlistId,\n maxResults: 25,\n pageToken: nextPageToken\n });\n\n for (var j = 0; j \u003c playlistResponse.items.length; j++) {\n var playlistItem = playlistResponse.items[j];\n console.log('[%s] Title: %s',\n playlistItem.snippet.resourceId.videoId,\n playlistItem.snippet.title);\n\n }\n nextPageToken = playlistResponse.nextPageToken;\n }\n }\n}\n```\n\nSearch videos by keyword\n------------------------\n\n```transact-sql\nfunction searchVideosByKeyword() {\n var results = YouTube.Search.list('id,snippet', {q: 'dogs', maxResults: 25});\n for (var i in results.items) {\n var item = results.items[i];\n console.log('[%s] Title: %s', item.id.videoId, item.snippet.title);\n }\n}\n```\n\nSearch videos by topics\n-----------------------\n\n```transact-sql\nfunction searchVideosByFreebaseTopic() {\n // See https://developers.google.com/youtube/v3/guides/searching_by_topic\n // for more details.\n\n // Insert Your Freebase topic ID here. The Freebase ID used in this example\n // corresponds to the Freebase entry for Google. See\n // http://www.freebase.com/m/045c7b for more details.\n var mid = '/m/045c7b';\n var results = YouTube.Search.list('id,snippet',\n {topicId: mid, maxResults: 25});\n for (var i in results.items) {\n var item = results.items[i];\n console.log('[%s] Title: %s', item.id.videoId, item.snippet.title);\n }\n}\n```\n\nSubscribe to a channel\n----------------------\n\n```gdscript\nfunction subscribeToChannel() {\n // Replace this channel ID with the channel ID you want to subscribe to.\n\n var channelId = 'INSERT_YOUTUBE_CHANNEL_ID_HERE';\n var resource = {\n snippet: {\n resourceId: {\n kind: 'youtube#channel',\n channelId: channelId\n }\n }\n };\n\n try {\n var response = YouTube.Subscriptions.insert(resource, 'snippet');\n\n console.log('Subscribed to channel ID %s successfully.', channelId);\n } catch (e) {\n if (e.message.match('subscriptionDuplicate')) {\n console.log('Cannot subscribe; already subscribed to channel: ' +\n channelId);\n } else {\n console.log('Error adding subscription: ' + e.message);\n }\n }\n}\n```"]]