/** * 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('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 APIconsole.log('Failed with an error %s',err.message);}}
업로드 가져오기
이 함수는 사용자가 업로드한 동영상을 가져옵니다. 이 작업은 다음 단계를 사용하여 수행됩니다.
/** * 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('No Channels found.');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('No Playlist found.');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('Failed with err %s',err.message);}}
/** * 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('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 exceptionconsole.log('Error adding subscription: '+e.message);}}}
[null,null,["최종 업데이트: 2025-08-31(UTC)"],[[["\u003cp\u003eThe 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.\u003c/p\u003e\n"],["\u003cp\u003eThis is an advanced service that needs to be enabled before use within your Apps Script project.\u003c/p\u003e\n"],["\u003cp\u003eSample code is provided to demonstrate searching for videos, retrieving user uploads, and subscribing to channels using the API.\u003c/p\u003e\n"],["\u003cp\u003eRefer to the YouTube Data API and YouTube Live Streaming API reference documentation for detailed information and further functionalities.\u003c/p\u003e\n"]]],[],null,["# YouTube Service\n\nThe YouTube service allows you to use the [YouTube Data API](/youtube/v3)\nand [YouTube Live Streaming API](/youtube/v3/live) in Apps Script. This API\ngives users the ability to manage their videos, playlists, channels, and live\nevents.\n| **Note:** This is an advanced service that must be [enabled before use](/apps-script/guides/services/advanced).\n\nReference\n---------\n\nFor detailed information on this service, see the following reference\ndocumentation:\n\n- [YouTube Data API](/youtube/v3/docs)\n- [YouTube Live Streaming API](/youtube/v3/live/docs)\n\nLike all advanced services in Apps Script, the YouTube service uses the same\nobjects, methods, and parameters as the public API. For more information, see [How method signatures are determined](/apps-script/guides/services/advanced#how_method_signatures_are_determined).\n\nTo report issues and find other support, see the corresponding support pages:\n\n- [YouTube Data API support](/youtube/v3/support)\n- [YouTube Live Streaming API support](/youtube/v3/live/support)\n\nSample code\n-----------\n\nThe sample code below uses [version 3](/youtube/v3/docs) of the YouTube Data\nAPI.\n\n### Search by keyword\n\nThis function searches for videos about dogs, then logs the video IDs and title.\nNote that this sample limits the results to 25. To return more results, pass\nadditional parameters as shown in the\n[YouTube Data API reference documentation](/youtube/v3/docs/search/list). \nadvanced/youtube.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/advanced/youtube.gs) \n\n```javascript\n/**\n * Searches for videos about dogs, then logs the video IDs and title.\n * Note that this sample limits the results to 25. To return more\n * results, pass additional parameters as shown in the YouTube Data API docs.\n * @see https://developers.google.com/youtube/v3/docs/search/list\n */\nfunction searchByKeyword() {\n try {\n const results = YouTube.Search.list('id,snippet', {\n q: 'dogs',\n maxResults: 25\n });\n if (results === null) {\n console.log('Unable to search videos');\n return;\n }\n results.items.forEach((item)=\u003e {\n console.log('[%s] Title: %s', item.id.videoId, item.snippet.title);\n });\n } catch (err) {\n // TODO (developer) - Handle exceptions from Youtube API\n console.log('Failed with an error %s', err.message);\n }\n}\n```\n\n### Retrieve uploads\n\nThis function retrieves the user's uploaded videos. It does this using the\nfollowing steps:\n\n1. Fetches the user's channel\n2. Fetches the user's `uploads` playlist\n3. Iterates through this playlist and logs the video IDs and titles\n4. If there is a next page of results, fetches it, then returns to step 3\n\nadvanced/youtube.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/advanced/youtube.gs) \n\n```javascript\n/**\n * This function retrieves the user's uploaded videos by:\n * 1. Fetching the user's channel's.\n * 2. Fetching the user's \"uploads\" playlist.\n * 3. Iterating through this playlist and logs the video IDs and titles.\n * 4. If there is a next page of resuts, fetching it and returns to step 3.\n */\nfunction retrieveMyUploads() {\n try {\n // @see https://developers.google.com/youtube/v3/docs/channels/list\n const results = YouTube.Channels.list('contentDetails', {\n mine: true\n });\n if (!results || results.items.length === 0) {\n console.log('No Channels found.');\n return;\n }\n for (let i = 0; i \u003c results.items.length; i++) {\n const item = results.items[i];\n /** Get the channel ID - it's nested in contentDetails, as described in the\n * Channel resource: https://developers.google.com/youtube/v3/docs/channels.\n */\n const playlistId = item.contentDetails.relatedPlaylists.uploads;\n let nextPageToken = null;\n do {\n // @see: https://developers.google.com/youtube/v3/docs/playlistItems/list\n const playlistResponse = YouTube.PlaylistItems.list('snippet', {\n playlistId: playlistId,\n maxResults: 25,\n pageToken: nextPageToken\n });\n if (!playlistResponse || playlistResponse.items.length === 0) {\n console.log('No Playlist found.');\n break;\n }\n for (let j = 0; j \u003c playlistResponse.items.length; j++) {\n const playlistItem = playlistResponse.items[j];\n console.log('[%s] Title: %s',\n playlistItem.snippet.resourceId.videoId,\n playlistItem.snippet.title);\n }\n nextPageToken = playlistResponse.nextPageToken;\n } while (nextPageToken);\n }\n } catch (err) {\n // TODO (developer) - Handle exception\n console.log('Failed with err %s', err.message);\n }\n}\n```\n\n### Subscribe to channel\n\nThis sample subscribes the user to the Google Developers channel on YouTube. \nadvanced/youtube.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/advanced/youtube.gs) \n\n```javascript\n/**\n * This sample subscribes the user to the Google Developers channel on YouTube.\n * @see https://developers.google.com/youtube/v3/docs/subscriptions/insert\n */\nfunction addSubscription() {\n // Replace this channel ID with the channel ID you want to subscribe to\n const channelId = 'UC_x5XG1OV2P6uZZ5FSM9Ttw';\n const resource = {\n snippet: {\n resourceId: {\n kind: 'youtube#channel',\n channelId: channelId\n }\n }\n };\n\n try {\n const response = YouTube.Subscriptions.insert(resource, 'snippet');\n console.log('Added subscription for channel title : %s', response.snippet.title);\n } catch (e) {\n if (e.message.match('subscriptionDuplicate')) {\n console.log('Cannot subscribe; already subscribed to channel: ' +\n channelId);\n } else {\n // TODO (developer) - Handle exception\n console.log('Error adding subscription: ' + e.message);\n }\n }\n}\n```"]]