This is done with python 2 becuase most of the Youtube DATA libraries seem to support Python 2 better than 3. There may be versions of some of the libraries for Python 3 but I did not spend anytime investigating this route.
This link goes over most of the setup process pretty well: https://developers.google.com/youtube/v3/getting-started
Go to the developers console and create a new project https://console.developers.google.com/
Now on the project page go to Credentials and create an OAuth 2 Client ID of type Other. Now download the json file which we will use to authenticate our app.
pip install --upgrade google-api-python-client
pip install --upgrade google-auth google-auth-oauthlib google-auth-httplib2
cramsan@kururu ~/git/cramsan.github.io/scripts $ python2 demo.py
Please visit this URL to authorize this application: https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=743336358314-o1grlivilob3pb4k3ra4b45kulgu20oa.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutube.readonly&state=plvu3RkeSZL8Laj3wtz6IHsuvXBgY9&prompt=consent&access_type=offline
Enter the authorization code:
Acessing the API is rather simple.
def get_authenticated_service():
flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRETS_FILE, SCOPES)
credentials = flow.run_console()
return build(API_SERVICE_NAME, API_VERSION, credentials = credentials)
youtube = get_authenticated_service()def get_my_uploads_list():
# Retrieve the contentDetails part of the channel resource for the
# authenticated user's channel.
channels_response = youtube.channels().list(
mine=True,
part='contentDetails'
).execute()
for channel in channels_response['items']:
# From the API response, extract the playlist ID that identifies the list
# of videos uploaded to the authenticated user's channel.
return channel['contentDetails']['relatedPlaylists']['uploads']
return Nonedef list_my_uploaded_videos(uploads_playlist_id):
# Retrieve the list of videos uploaded to the authenticated user's channel.
playlistitems_list_request = youtube.playlistItems().list(
playlistId=uploads_playlist_id,
part='snippet',
maxResults=5
)
print 'Videos in list %s' % uploads_playlist_id
while playlistitems_list_request:
playlistitems_list_response = playlistitems_list_request.execute()
# Print information about each video.
for playlist_item in playlistitems_list_response['items']:
title = playlist_item['snippet']['title']
video_id = playlist_item['snippet']['resourceId']['videoId']
print '%s (%s)' % (title, video_id)
playlistitems_list_request = youtube.playlistItems().list_next(
playlistitems_list_request, playlistitems_list_response)