Skip to content

Commit acb6446

Browse files
"Added sample: python/playlist_updates.py"
1 parent 59de295 commit acb6446

1 file changed

Lines changed: 77 additions & 0 deletions

File tree

python/playlist_updates.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#!/usr/bin/python
2+
3+
import httplib2
4+
import os
5+
import sys
6+
7+
from apiclient.discovery import build
8+
from apiclient.errors import HttpError
9+
from oauth2client.client import flow_from_clientsecrets
10+
from oauth2client.file import Storage
11+
from oauth2client.tools import argparser, run_flow
12+
13+
14+
# The CLIENT_SECRETS_FILE variable specifies the name of a file that contains
15+
# the OAuth 2.0 information for this application, including its client_id and
16+
# client_secret. You can acquire an OAuth 2.0 client ID and client secret from
17+
# the {{ Google Cloud Console }} at
18+
# {{ https://cloud.google.com/console }}.
19+
# Please ensure that you have enabled the YouTube Data API for your project.
20+
# For more information about using OAuth2 to access the YouTube Data API, see:
21+
# https://developers.google.com/youtube/v3/guides/authentication
22+
# For more information about the client_secrets.json file format, see:
23+
# https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
24+
CLIENT_SECRETS_FILE = "client_secrets.json"
25+
26+
# This variable defines a message to display if the CLIENT_SECRETS_FILE is
27+
# missing.
28+
MISSING_CLIENT_SECRETS_MESSAGE = """
29+
WARNING: Please configure OAuth 2.0
30+
31+
To make this sample run you will need to populate the client_secrets.json file
32+
found at:
33+
34+
%s
35+
36+
with information from the {{ Cloud Console }}
37+
{{ https://cloud.google.com/console }}
38+
39+
For more information about the client_secrets.json file format, please visit:
40+
https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
41+
""" % os.path.abspath(os.path.join(os.path.dirname(__file__),
42+
CLIENT_SECRETS_FILE))
43+
44+
# This OAuth 2.0 access scope allows for full read/write access to the
45+
# authenticated user's account.
46+
YOUTUBE_READ_WRITE_SCOPE = "https://www.googleapis.com/auth/youtube"
47+
YOUTUBE_API_SERVICE_NAME = "youtube"
48+
YOUTUBE_API_VERSION = "v3"
49+
50+
flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE,
51+
message=MISSING_CLIENT_SECRETS_MESSAGE,
52+
scope=YOUTUBE_READ_WRITE_SCOPE)
53+
54+
storage = Storage("%s-oauth2.json" % sys.argv[0])
55+
credentials = storage.get()
56+
57+
if credentials is None or credentials.invalid:
58+
credentials = run(flow, storage)
59+
60+
youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
61+
http=credentials.authorize(httplib2.Http()))
62+
63+
# This code creates a new, private playlist in the authorized user's channel.
64+
playlists_insert_response = youtube.playlists().insert(
65+
part="snippet,status",
66+
body=dict(
67+
snippet=dict(
68+
title="Test Playlist",
69+
description="A private playlist created with the YouTube API v3"
70+
),
71+
status=dict(
72+
privacyStatus="private"
73+
)
74+
)
75+
).execute()
76+
77+
print "New playlist id: %s" % playlists_insert_response["id"]

0 commit comments

Comments
 (0)