## Prerequisites
* Python 2.6 or greater
* The pip package management tool
* The Google APIs Client Library for Python:
```
pip install --upgrade google-api-python-client
```
* The google-auth, google-auth-oauthlib, and google-auth-httplib2 for user authorization.
```
pip install --upgrade google-auth google-auth-oauthlib google-auth-httplib2
```
### Setting up your project and running code samples
1. Create a project in the API Console and set up credentials for a web application. Set the authorized redirect URIs as appropriate.
2. Save the client_secrets.json file associated with your credentials to a local file.
3. Copy the full code sample to a local file in the same directory as the client_secrets.json file (or modify the sample to correctly identify that file's location.
4. Run the sample from the command line and set command-line arguments as necessary:
```python sample.py --arg1=value1 --arg2=value2 ...```
5. Most samples print something to STDOUT. You can also check the YouTube website to see the effects of requests that write data, such as requests that create playlists or channel sections.
## Samples in this directory:
### [Add a channel section](/python/add_channel_section.py)
Method: youtube.channelSections.insert
Description: This sample calls the API's channelSections.insert method to create channel sections.
The code accepts a number of command line arguments that let you specify the section's type, display style, title, position,
and content.
This sample also updates the channel's
brandingSettings.channel.showBrowseView
property so that the channel displays content in a browse view (rather than a feed view). A channel's sections are only
visible if the channel displays content in a browse view.
More information on channel sections is available in the
YouTube Help Center.
### [Add a channel subscription](/python/add_subscription.py)
Method: youtube.subscriptions.insert
Description: This sample calls the API's subscriptions.insert method to add a subscription to a specified
channel.
### [Add a featured video](/python/add_featured_video.py)
Method: youtube.channels.update
Description: This sample calls the API's channels.update method to set invideoPromotion
properties for the channel.
### [Create a playlist](/python/playlist_updates.py)
Method: youtube.playlists.insert
Description: This sample calls the API's playlists.insert method to create a private playlist owned by the
channel authorizing the request.
### [Create and manage comments](/python/comment_handling.py)
Method: youtube.commentThreads.list, youtube.comments.insert, youtube.comments.list, youtube.comments.update,
youtube.comments.setModerationStatus, youtube.comments.markAsSpam, youtube.comments.delete
Description: This sample demonstrates how to use the following API methods to create and manage comments:
commentThreads.list method with the videoId parameter set to retrieve comments
for a video.comments.insert method with the parentId parameter set to reply to an existing
comment.comments.list method with the parentId parameter to retrieve the comments in the
thread.comments.update method with comment in the request body to update a comment.comments.setModerationStatus method to set the moderation status of the comment, the
comments.markAsSpam method to mark the comment as spam, and the comments.delete method to
delete the comment, using the id parameter to identify the comment.commentThreads.insert method once with the channelId parameter to create a
channel comment and once with the videoId parameter to create a video comment.commentThreads.list method once with the channelId parameter to retrieve
channel comments and once with the videoId parameter to retrieve video comments.commentThreads.update method once to update a video comment and then again to update a
channel comment. In each case, the request body contains the comment resource being updated.captions.insert method with the isDraft parameter set to true
to upload a caption track in draft status.captions.list method with the videoId parameter to retrieve video caption
tracks.captions.update method with the caption in the request body to update a caption track.captions.download method to download the caption track.captions.delete method to delete the caption track, using the id parameter to
identify the caption track.videos.rate method to set a positive rating for a video.
### [Post a channel bulletin](/python/channel_bulletin.py)
Method: youtube.activities.insertactivities.insert method to post a bulletin to the channel
associated with the request.
### [Remove a watermark image from a channel](/python/unset_watermark.py)
Method: youtube.watermarks.unsetwatermarks.unset method to remove the watermark
image for a channel. The request must be authorized by the channel that owns the video.
### [Retrieve my uploads](/python/my_uploads.py)
Method: youtube.playlistItems.listplaylistItems.list method to retrieve a list of videos uploaded
to the channel associated with the request. The code also calls the channels.list method with the
mine parameter set to true to retrieve the playlist ID that identifies the channel's uploaded
videos.
### [Search by keyword](/python/search.py)
Method: youtube.search.listsearch.list method to retrieve search results associated with
a particular keyword.
### [Search by location](/python/geolocation_search.py)
Method: youtube.search.list, youtube.videos.listsearch.list method with the type,
q, location, and locationRadius parameters to retrieve search results
matching the provided keyword within the radius centered at a particular location. Using the video ids from
the search result, the sample calls the API's videos.list method to retrieve location details
of each video.
### [Set and retrieve localized channel metadata](/python/channel_localizations.py)
Method: youtube.channels.update, youtube.channels.listchannels.update method to update the default language of a channel's metadata and to add a
localized version of this metadata in a selected language. Note that to set the default language for a channel resource,
you actually need to update the brandingSettings.channel.defaultLanguage property.channels.list method with the hl parameter set to a specific language to
retrieve localized metadata in that language.channels.list method and includes localizations in the part
parameter value to retrieve all of the localized metadata for that channel.channelSections.update method to update the default language of a channel section's
metadata and to add a localized version of this metadata in a selected language.channelSections.list method with the hl parameter set to a specific language
to retrieve localized metadata in that language.channelSections.list method and includes localizations in the
part parameter value to retrieve all of the localized metadata for that channel section.playlists.update method to update the default language of a playlist's metadata and to add
a localized version of this metadata in a selected language.playlists.list method with the hl parameter set to a specific language to
retrieve localized metadata in that language.playlists.list method and includes localizations in the part
parameter value to retrieve all of the localized metadata for that playlist.videos.update method to update the default language of a video's metadata and to add
a localized version of this metadata in a selected language.videos.list method with the hl parameter set to a specific language to
retrieve localized metadata in that language.videos.list method and includes localizations in the part
parameter value to retrieve all of the localized metadata for that video.channelSections.list method to get the list of current channel
sections. Then it shuffles the list and calls channelSections.update to change the position of each item.videos.update method to update a video owned by the channel
authorizing the request.
### [Upload a banner image and set as channel's banner](/python/upload_banner.py)
Method: youtube.channelBanners.insert, youtube.channels.updatechannelBanners.insert method to upload an image. With the
returned URL, the sample calls channels.update method to update the channel's banner to that image.
### [Upload a video](/python/upload_video.py)
Method: youtube.videos.insertvideos.insert method to upload a video to the channel associated
with the request.
### [Upload a video thumbnail image](/python/upload_thumbnail.py)
Method: youtube.thumbnails.setthumbnails.set method to upload an image and set it as the
thumbnail image for a video. The request must be authorized by the channel that owns the video.
### [Upload a watermark image and set it for a channel](/python/set_watermark.py)
Method: youtube.watermarks.setwatermarks.set method to upload an image and set it as the
watermark image for a channel. The request must be authorized by the channel that owns the video.
### [Create a broadcast and stream](/python/create_broadcast.py)
Method: youtube.liveBroadcasts.bind,youtube.liveBroadcasts.insert,youtube.liveStreams.insertliveBroadcasts.insert and liveStreams.insert
methods to create a broadcast and a stream. Then, it calls the liveBroadcasts.bind method to bind
the stream to the broadcast.
### [Retrieve a channel's broadcasts](/python/list_broadcasts.py)
Method: youtube.liveBroadcasts.listliveBroadcasts.list method to retrieve a list of broadcasts for
the channel associated with the request. By default, the request retrieves all broadcasts for the channel, but you can
also specify a value for the --broadcast-status option to only retrieve broadcasts with a particular status.
### [Retrieve a channel's live video streams](/python/list_streams.py)
Method: youtube.liveStreams.listliveStreams.list method to retrieve a list of video stream settings
that a channel can use to broadcast live events on YouTube.
### [Retrieve top 10 videos by viewcount](/python/yt_analytics_report.py)
Method: youtubeAnalytics.reports.queryreports.query method to retrieve YouTube Analytics data.
By default, the report retrieves the top 10 videos based on viewcounts, and it returns several metrics for those
videos, sorting the results in reverse order by viewcount. By setting command line parameters, you can use the
same code to retrieve other reports as well.
### [Create a reporting job](/python/create_reporting_job.py)
Method: youtubeReporting.reportTypes.list, youtubeReporting.jobs.createreportTypes.list method
to retrieve a list of available report types. It then calls the jobs.create method to create a new reporting
job.
### [Retrieve reports](/python/retrieve_reports.py)
Method: youtubeReporting.jobs.list, youtubeReporting.reports.listjobs.list method to retrieve reporting jobs. It then calls the reports.list method with the
jobId parameter set to a specific job id to retrieve reports created by that job. Finally, the sample
prints out the download URL for each report.