Skip to content

Commit 74c2ef9

Browse files
"Added sample: python/my_uploads.py"
1 parent b4a5ff8 commit 74c2ef9

1 file changed

Lines changed: 96 additions & 0 deletions

File tree

python/my_uploads.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#!/usr/bin/python
2+
3+
import httplib2
4+
import os
5+
import sys
6+
7+
from apiclient.discovery import build
8+
from oauth2client.client import flow_from_clientsecrets
9+
from oauth2client.file import Storage
10+
from oauth2client.tools import argparser, run_flow
11+
12+
13+
# The CLIENT_SECRETS_FILE variable specifies the name of a file that contains
14+
# the OAuth 2.0 information for this application, including its client_id and
15+
# client_secret. You can acquire an OAuth 2.0 client ID and client secret from
16+
# the {{ Google Cloud Console }} at
17+
# {{ https://cloud.google.com/console }}.
18+
# Please ensure that you have enabled the YouTube Data API for your project.
19+
# For more information about using OAuth2 to access the YouTube Data API, see:
20+
# https://developers.google.com/youtube/v3/guides/authentication
21+
# For more information about the client_secrets.json file format, see:
22+
# https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
23+
CLIENT_SECRETS_FILE = "client_secrets.json"
24+
25+
# This variable defines a message to display if the CLIENT_SECRETS_FILE is
26+
# missing.
27+
MISSING_CLIENT_SECRETS_MESSAGE = """
28+
WARNING: Please configure OAuth 2.0
29+
30+
To make this sample run you will need to populate the client_secrets.json file
31+
found at:
32+
33+
%s
34+
35+
with information from the {{ Cloud Console }}
36+
{{ https://cloud.google.com/console }}
37+
38+
For more information about the client_secrets.json file format, please visit:
39+
https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
40+
""" % os.path.abspath(os.path.join(os.path.dirname(__file__),
41+
CLIENT_SECRETS_FILE))
42+
43+
# This OAuth 2.0 access scope allows for read-only access to the authenticated
44+
# user's account, but not other types of account access.
45+
YOUTUBE_READONLY_SCOPE = "https://www.googleapis.com/auth/youtube.readonly"
46+
YOUTUBE_API_SERVICE_NAME = "youtube"
47+
YOUTUBE_API_VERSION = "v3"
48+
49+
flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE,
50+
message=MISSING_CLIENT_SECRETS_MESSAGE,
51+
scope=YOUTUBE_READONLY_SCOPE)
52+
53+
storage = Storage("%s-oauth2.json" % sys.argv[0])
54+
credentials = storage.get()
55+
56+
if credentials is None or credentials.invalid:
57+
flags = argparser.parse_args()
58+
credentials = run_flow(flow, storage, flags)
59+
60+
youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
61+
http=credentials.authorize(httplib2.Http()))
62+
63+
# Retrieve the contentDetails part of the channel resource for the
64+
# authenticated user's channel.
65+
channels_response = youtube.channels().list(
66+
mine=True,
67+
part="contentDetails"
68+
).execute()
69+
70+
for channel in channels_response["items"]:
71+
# From the API response, extract the playlist ID that identifies the list
72+
# of videos uploaded to the authenticated user's channel.
73+
uploads_list_id = channel["contentDetails"]["relatedPlaylists"]["uploads"]
74+
75+
print "Videos in list %s" % uploads_list_id
76+
77+
# Retrieve the list of videos uploaded to the authenticated user's channel.
78+
playlistitems_list_request = youtube.playlistItems().list(
79+
playlistId=uploads_list_id,
80+
part="snippet",
81+
maxResults=50
82+
)
83+
84+
while playlistitems_list_request:
85+
playlistitems_list_response = playlistitems_list_request.execute()
86+
87+
# Print information about each video.
88+
for playlist_item in playlistitems_list_response["items"]:
89+
title = playlist_item["snippet"]["title"]
90+
video_id = playlist_item["snippet"]["resourceId"]["videoId"]
91+
print "%s (%s)" % (title, video_id)
92+
93+
playlistitems_list_request = youtube.playlistItems().list_next(
94+
playlistitems_list_request, playlistitems_list_response)
95+
96+
print

0 commit comments

Comments
 (0)