YouTube सेवा

YouTube सेवा आपको Apps Script में YouTube Data API और YouTube लाइव स्ट्रीमिंग एपीआई इस्तेमाल करने की अनुमति देती है. इस एपीआई की मदद से उपयोगकर्ताओं को अपने वीडियो, प्लेलिस्ट, चैनल, और लाइव इवेंट को मैनेज करने की सुविधा मिलती है.

रेफ़रंस

इस सेवा के बारे में ज़्यादा जानकारी के लिए, नीचे दिए गए रेफ़रंस दस्तावेज़ देखें:

Apps Script की सभी बेहतर सेवाओं की तरह, YouTube सेवा में भी उन ही चीज़ों, तरीकों, और पैरामीटर का इस्तेमाल किया जाता है जो सार्वजनिक एपीआई में हैं. ज़्यादा जानकारी के लिए, हस्ताक्षर तय करने का तरीका लेख पढ़ें.

समस्याओं की रिपोर्ट करने और अन्य सहायता ढूंढने के लिए, संबंधित सहायता पेज देखें:

नमूना कोड

नीचे दिए गए सैंपल कोड में, YouTube Data API के वर्शन 3 का इस्तेमाल किया गया है.

कीवर्ड से खोजें

यह फ़ंक्शन, कुत्तों के बारे में वीडियो खोजता है. इसके बाद, वीडियो के आईडी और टाइटल को लॉग करता है. ध्यान दें कि इस सैंपल में, नतीजों की संख्या 25 दिखाई गई है. ज़्यादा नतीजे दिखाने के लिए, YouTube Data API रेफ़रंस दस्तावेज़ में बताए गए अतिरिक्त पैरामीटर पास करें.

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. अगर नतीजों का कोई अगला पेज है, तो उसे फ़ेच करता है. इसके बाद, वापस तीसरे चरण पर जाता है
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);
  }
}

चैनल की सदस्यता लें

यह सैंपल, उपयोगकर्ता को YouTube पर Google Developers चैनल की सदस्यता देता है.

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);
    }
  }
}