บริการของ YouTube

บริการ YouTube อนุญาตให้คุณใช้ YouTube Data API และ YouTube Live Streaming API ใน Apps Script API นี้ช่วยให้ผู้ใช้จัดการวิดีโอ เพลย์ลิสต์ ช่อง และการถ่ายทอดสดได้

ข้อมูลอ้างอิง

สำหรับข้อมูลโดยละเอียดเกี่ยวกับบริการนี้ โปรดดูเอกสารอ้างอิงต่อไปนี้

บริการ YouTube ใช้ออบเจ็กต์ เมธอด และพารามิเตอร์เดียวกันกับ API สาธารณะ เช่นเดียวกับบริการขั้นสูงทั้งหมดใน Apps Script ดูข้อมูลเพิ่มเติมได้ที่วิธีกำหนดลายเซ็นของเมธอด

หากต้องการรายงานปัญหาและค้นหาการสนับสนุนอื่นๆ โปรดดูหน้าการสนับสนุนที่เกี่ยวข้อง

รหัสตัวอย่าง

โค้ดตัวอย่างด้านล่างใช้ YouTube Data API เวอร์ชัน 3

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

ฟังก์ชันนี้จะค้นหาวิดีโอเกี่ยวกับสุนัข จากนั้นบันทึกรหัสวิดีโอและชื่อ โปรดทราบว่าตัวอย่างนี้จำกัดผลลัพธ์ไว้ที่ 25 รายการ หากต้องการแสดงผลการค้นหาเพิ่มเติม ให้ส่งพารามิเตอร์เพิ่มเติมดังที่แสดงในเอกสารอ้างอิง API ข้อมูลของ YouTube

advanced/youtube.gs
/**
 * 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
 */
function searchByKeyword() {
  try {
    const results = YouTube.Search.list('id,snippet', {
      q: 'dogs',
      maxResults: 25
    });
    if (results === null) {
      console.log('Unable to search videos');
      return;
    }
    results.items.forEach((item)=> {
      console.log('[%s] Title: %s', item.id.videoId, item.snippet.title);
    });
  } catch (err) {
    // TODO (developer) - Handle exceptions from Youtube API
    console.log('Failed with an error %s', err.message);
  }
}

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

ฟังก์ชันนี้จะดึงข้อมูลวิดีโอที่ผู้ใช้อัปโหลด โดยทำตามขั้นตอนต่อไปนี้

  1. ดึงข้อมูลช่องของผู้ใช้
  2. ดึงข้อมูลเพลย์ลิสต์ uploads ของผู้ใช้
  3. ทำซ้ำผ่านเพลย์ลิสต์นี้และบันทึกรหัสวิดีโอและชื่อ
  4. หากมีผลการค้นหาหน้าถัดไป ให้ดึงข้อมูลแล้วกลับไปที่ขั้นตอนที่ 3
advanced/youtube.gs
/**
 * 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.
 */
function retrieveMyUploads() {
  try {
    // @see https://developers.google.com/youtube/v3/docs/channels/list
    const results = YouTube.Channels.list('contentDetails', {
      mine: true
    });
    if (!results || results.items.length === 0) {
      console.log('No Channels found.');
      return;
    }
    for (let i = 0; i < results.items.length; i++) {
      const item = 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.
       */
      const playlistId = item.contentDetails.relatedPlaylists.uploads;
      let nextPageToken = null;
      do {
        // @see: https://developers.google.com/youtube/v3/docs/playlistItems/list
        const playlistResponse = YouTube.PlaylistItems.list('snippet', {
          playlistId: playlistId,
          maxResults: 25,
          pageToken: nextPageToken
        });
        if (!playlistResponse || playlistResponse.items.length === 0) {
          console.log('No Playlist found.');
          break;
        }
        for (let j = 0; j < playlistResponse.items.length; j++) {
          const playlistItem = 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 exception
    console.log('Failed with err %s', err.message);
  }
}

ติดตามช่อง

ต��วอย่างนี้ติดตามช่อง Google Developers ใน YouTube ให้กับผู้ใช้

advanced/youtube.gs
/**
 * This sample subscribes the user to the Google Developers channel on YouTube.
 * @see https://developers.google.com/youtube/v3/docs/subscriptions/insert
 */
function addSubscription() {
  // Replace this channel ID with the channel ID you want to subscribe to
  const channelId = 'UC_x5XG1OV2P6uZZ5FSM9Ttw';
  const resource = {
    snippet: {
      resourceId: {
        kind: 'youtube#channel',
        channelId: channelId
      }
    }
  };

  try {
    const response = YouTube.Subscriptions.insert(resource, 'snippet');
    console.log('Added subscription for channel title : %s', response.snippet.title);
  } catch (e) {
    if (e.message.match('subscriptionDuplicate')) {
      console.log('Cannot subscribe; already subscribed to channel: ' +
        channelId);
    } else {
      // TODO (developer) - Handle exception
      console.log('Error adding subscription: ' + e.message);
    }
  }
}