ตัวอย่างโค้ด Apps Script

ตัวอย่างโค้ด Apps Script ต่อไปนี้พร้อมใช้งานใน YouTube Data API คุณดาวน์โหลดตัวอย่างโค้ดเหล่านี้ได้จากโฟลเดอร์ apps-script ของที่เก็บตัวอย่างโค้ด YouTube API ใน GitHub

เรียกข้อมูลการอัปโหลดของฉัน

ฟังก์ชันนี้จะดึงข้อมูลวิดีโอที่ผู้ใช้สคริปต์ปัจจุบันอัปโหลด การดำเนินการนี้ต้องใช้ขอบเขตการอ่าน/เขียน OAuth สำหรับ YouTube รวมถึงการให้สิทธิ์ของผู้ใช้ ในสภาพแวดล้อมรันไทม์ของ Apps Script เมื่อผู้ใช้เรียกใช้สคริปต์เป็นครั้งแรก Apps Script จะแจ้งให้ผู้ใช้อนุญาตสิทธิ์เข้าถึงบริการที่สคริปต์เรียกใช้ หลังจากให้สิทธิ์แล้ว ระบบจะแคชสิทธิ์ไว้เป็นระยะเวลาหนึ่ง ระบบจะแจ้งให้ผู้ใช้เรียกใช้สคริปต์ขอสิทธิ์อีกครั้งเมื่อสิทธิ์ที่จำเป็นมีการเปลี่ยนแปลง หรือเมื่อสิทธิ์ถูกทำให้ใช้งานไม่ได้โดยฟังก์ชัน ScriptApp.invalidateAuth()

สคริปต์นี้จะดึงข้อมูลวิดีโอที่อัปโหลดของผู้ใช้ที่ใช้งานอยู่โดยทำตามขั้นตอนต่อไปนี้
  1. ดึงข้อมูลช่องของผู้ใช้
  2. ดึงข้อมูลเพลย์ลิสต์ "การอัปโหลด" ของผู้ใช้
  3. เรียกใช้เพลย์ลิสต์นี้ซ้ำและบันทึกรหัสและชื่อวิดีโอ
  4. เรียกข้อมูลโทเค็นหน้าถัดไป หากมี เรียกข้อมูลหน้าถัดไป หากมี ทำขั้นตอนที่ 3 ซ้ำ
/**  * This function retrieves the current script user's uploaded videos. To execute,  * it requires the OAuth read/write scope for YouTube as well as user authorization.  * In Apps Script's runtime environment, the first time a user runs a script, Apps  * Script will prompt the user for permission to access the services called by the  * script. After permissions are granted, they are cached for some periodF of time.  * The user running the script will be prompted for permission again once the  * permissions required change, or when they are invalidated by the  * ScriptApp.invalidateAuth() function.  *  * This script takes the following steps to retrieve the active user's uploaded videos:  *    1. Fetches the user's channels  *    2. Fetches the user's 'uploads' playlist  *    3. Iterates through this playlist and logs the video IDs and titles  *    4. Fetches a next page token (if any). If there is one, fetches the next page. GOTO Step 3  */ function retrieveMyUploads() {   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];         Logger.log('[%s] Title: %s',                    playlistItem.snippet.resourceId.videoId,                    playlistItem.snippet.title);        }       nextPageToken = playlistResponse.nextPageToken;     }   } }

ค้นหาตามคีย์เวิร์ด

ฟังก์ชันนี้จะค้นหาวิดีโอที่เกี่ยวข้องกับคีย์เวิร์ด 'dogs' ระบบจะบันทึกรหัสและชื่อวิดีโอของผลการค้นหาลงในบันทึกของ Apps Script

โปรดทราบว่าตัวอย่างนี้จำกัดผลลัพธ์ไว้ที่ 25 รายการ หากต้องการแสดงผลลัพธ์เพิ่มเติม ให้ส่งพารามิเตอร์เพิ่มเติมตามที่ระบุไว้ใน Search:list
/**  * This function searches for videos related to the keyword 'dogs'. The video IDs and titles  * of the search results are logged to Apps Script's log.  *  * Note that this sample limits the results to 25. To return more results, pass  * additional parameters as documented here:  *   https://developers.google.com/youtube/v3/docs/search/list  */ function searchByKeyword() {   var results = YouTube.Search.list('id,snippet', {q: 'dogs', maxResults: 25});   for(var i in results.items) {     var item = results.items[i];     Logger.log('[%s] Title: %s', item.id.videoId, item.snippet.title);   } }

ค้นหาตามหัวข้อ

ฟังก์ชันนี้จะค้นหาวิดีโอที่เชื่อมโยงกับหัวข้อ Freebase หนึ่งๆ โดยบันทึกรหัสและชื่อวิดีโอลงในบันทึกของ Apps Script ตัวอย่างนี้ใช้รหัสหัวข้อสําหรับ Google Apps Script

โปรดทราบว่าตัวอย่างนี้จำกัดผลลัพธ์ไว้ที่ 25 รายการ หากต้องการแสดงผลลัพธ์เพิ่มเติม ให้ส่งพารามิเตอร์เพิ่มเติมตามที่ระบุไว้ใน Search:list
/**  * This function searches for videos that are associated with a particular Freebase  * topic, logging their video IDs and titles to the Apps Script log. This example uses  * the topic ID for Google Apps Script.  *  * Note that this sample limits the results to 25. To return more results, pass  * additional parameters as documented here:  *   https://developers.google.com/youtube/v3/docs/search/list  */ function searchByTopic() {   var mid = '/m/0gjf126';   var results = YouTube.Search.list('id,snippet', {topicId: mid, maxResults: 25});   for(var i in results.items) {     var item = results.items[i];     Logger.log('[%s] Title: %s', item.id.videoId, item.snippet.title);   } }

ติดตามช่อง

ตัวอย่างนี้จะติดตามผู้ใช้ที่ใช้งานอยู่ให้ช่อง YouTube ของ Google Developers ที่ระบุโดย channelId
/**  * This sample subscribes the active user to the Google Developers  * YouTube channel, specified by the channelId.  */ function addSubscription() {   // Replace this channel ID with the channel ID you want to subscribe to   var channelId = 'UC_x5XG1OV2P6uZZ5FSM9Ttw';   var resource = {     snippet: {       resourceId: {         kind: 'youtube#channel',         channelId: channelId       }     }   };    try {     var response = YouTube.Subscriptions.insert(resource, 'snippet');     Logger.log(response);   } catch (e) {     if(e.message.match('subscriptionDuplicate')) {       Logger.log('Cannot subscribe; already subscribed to channel: ' + channelId);     } else {       Logger.log('Error adding subscription: ' + e.message);     }   } }

อัปเดตวิดีโอ

ตัวอย่างนี้จะค้นหาการอัปโหลดของผู้ใช้ที่ใช้งานอยู่ จากนั้นอัปเดตคำอธิบายของการอัปโหลดล่าสุดโดยต่อสตริง
/**  * 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');   } }