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);
    }
  }
}
  
  
  
  
    
  
 
  
    
      
      
    
    
      
    
    
  
       
    
    
      
    
  
  
  如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
  最后更新时间 (UTC):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"]],["最后更新时间 (UTC):2025-08-21。"],[],[]]