Dịch vụ YouTube cho phép bạn sử dụng API Dữ liệu YouTube và API Phát trực tiếp trên YouTube trong Apps Script. API này cho phép người dùng quản lý video, danh sách phát, kênh và sự kiện phát trực tiếp.
Tài liệu tham khảo
Để biết thông tin chi tiết về dịch vụ này, hãy xem tài liệu tham khảo sau:
Giống như tất cả các dịch vụ nâng cao trong Apps Script, dịch vụ YouTube sử dụng các đối tượng, phương thức và thông số giống như API công khai. Để biết thêm thông tin, hãy xem phần Cách xác định chữ ký phương thức.
Để báo cáo vấn đề và tìm các dịch vụ hỗ trợ khác, hãy xem các trang hỗ trợ tương ứng:
Mã mẫu bên dưới sử dụng phiên bản 3 của API Dữ liệu YouTube.
Tìm kiếm theo từ khoá
Hàm này tìm kiếm các video về chó, sau đó ghi lại mã và tiêu đề video.
Xin lưu ý rằng mẫu này giới hạn kết quả ở mức 25. Để trả về nhiều kết quả hơn, hãy truyền thêm các tham số như trong tài liệu tham khảo về YouTube Data API.
/** * Searches for videos about dogs, then logs the video IDs and title. * Note that this sample limits the results to 25. To return more * results, pass additional parameters as shown in the YouTube Data API docs. * @see https://developers.google.com/youtube/v3/docs/search/list */functionsearchByKeyword(){try{constresults=YouTube.Search.list('id,snippet',{q:'dogs',maxResults:25});if(results===null){console.log('Unabletosearchvideos');return;}results.items.forEach((item)=>{console.log('[%s]Title:%s',item.id.videoId,item.snippet.title);});}catch(err){// TODO (developer) - Handle exceptions from Youtube APIconsole.log('Failedwithanerror%s',err.message);}}
Truy xuất nội dung tải lên
Hàm này truy xuất các video do người dùng tải lên. Để thực hiện việc này, hãy làm theo các bước sau:
Tìm nạp kênh của người dùng
Tìm nạp danh sách phát uploads của người dùng
Lặp lại danh sách phát này và ghi lại mã và tiêu đề video
Nếu có trang kết quả tiếp theo, hãy tìm nạp trang đó rồi quay lại bước 3
/** * This function retrieves the user's uploaded videos by: * 1. Fetching the user's channel's. * 2. Fetching the user's "uploads" playlist. * 3. Iterating through this playlist and logs the video IDs and titles. * 4. If there is a next page of resuts, fetching it and returns to step 3. */functionretrieveMyUploads(){try{// @see https://developers.google.com/youtube/v3/docs/channels/listconstresults=YouTube.Channels.list('contentDetails',{mine:true});if(!results||results.items.length===0){console.log('NoChannelsfound.');return;}for(leti=0;i < results.items.length;i++){constitem=results.items[i];/** Get the channel ID - it's nested in contentDetails, as described in the * Channel resource: https://developers.google.com/youtube/v3/docs/channels. */constplaylistId=item.contentDetails.relatedPlaylists.uploads;letnextPageToken=null;do{// @see: https://developers.google.com/youtube/v3/docs/playlistItems/listconstplaylistResponse=YouTube.PlaylistItems.list('snippet',{playlistId:playlistId,maxResults:25,pageToken:nextPageToken});if(!playlistResponse||playlistResponse.items.length===0){console.log('NoPlaylistfound.');break;}for(letj=0;j < playlistResponse.items.length;j++){constplaylistItem=playlistResponse.items[j];console.log('[%s]Title:%s',playlistItem.snippet.resourceId.videoId,playlistItem.snippet.title);}nextPageToken=playlistResponse.nextPageToken;}while(nextPageToken);}}catch(err){// TODO (developer) - Handle exceptionconsole.log('Failedwitherr%s',err.message);}}
Đăng ký kênh
Mẫu này đăng ký người dùng theo dõi kênh Google Developers trên YouTube.
/** * This sample subscribes the user to the Google Developers channel on YouTube. * @see https://developers.google.com/youtube/v3/docs/subscriptions/insert */functionaddSubscription(){// Replace this channel ID with the channel ID you want to subscribe toconstchannelId='UC_x5XG1OV2P6uZZ5FSM9Ttw';constresource={snippet:{resourceId:{kind:'youtube#channel',channelId:channelId}}};try{constresponse=YouTube.Subscriptions.insert(resource,'snippet');console.log('Addedsubscriptionforchanneltitle:%s',response.snippet.title);}catch(e){if(e.message.match('subscriptionDuplicate')){console.log('Cannotsubscribe;alreadysubscribedtochannel:'+channelId);}else{// TODO (developer) - Handle exceptionconsole.log('Erroraddingsubscription:'+e.message);}}}
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2024-12-21 UTC."],[[["The YouTube service in Apps Script allows you to manage videos, playlists, channels, and live events using the YouTube Data API and YouTube Live Streaming API."],["This is an advanced service that needs to be enabled before use within your Apps Script project."],["Sample code is provided to demonstrate searching for videos, retrieving user uploads, and subscribing to channels using the API."],["Refer to the YouTube Data API and YouTube Live Streaming API reference documentation for detailed information and further functionalities."]]],[]]