Skip to content

Commit 7a7aa3e

Browse files
Update auth libraries, add title/description args
The oauth2client library is deprecated. This update changes the sample to use the google-auth and google-auth-oauthlib libraries instead. The changes also add --title and --description arguments for the playlist title and description. A final note is that this script seems to be misnamed. This change did not change the functionality of the script, but it's likely a future change will either make this script update a playlist rather than insert one or provide an option for selecting an operation to perform for a playlist.
1 parent 2ac0c7e commit 7a7aa3e

File tree

1 file changed

+56
-56
lines changed

1 file changed

+56
-56
lines changed

python/playlist_updates.py

Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
#!/usr/bin/python
22

3-
import httplib2
3+
# This code sample creates a private playlist in the authorizing user's
4+
# YouTube channel.
5+
# Usage:
6+
# python playlist_updates.py --title=<TITLE> --description=<DESCRIPTION>
7+
8+
import argparse
49
import os
5-
import sys
610

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
11+
import google.oauth2.credentials
12+
import google_auth_oauthlib.flow
13+
from googleapiclient.discovery import build
14+
from googleapiclient.errors import HttpError
15+
from google_auth_oauthlib.flow import InstalledAppFlow
1216

1317

1418
# The CLIENT_SECRETS_FILE variable specifies the name of a file that contains
@@ -21,58 +25,54 @@
2125
# https://developers.google.com/youtube/v3/guides/authentication
2226
# For more information about the client_secrets.json file format, see:
2327
# 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))
28+
29+
CLIENT_SECRETS_FILE = 'client_secret.json'
4330

4431
# This OAuth 2.0 access scope allows for full read/write access to the
4532
# 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-
flags = argparser.parse_args()
59-
credentials = run_flow(flow, storage, flags)
60-
61-
youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
62-
http=credentials.authorize(httplib2.Http()))
63-
64-
# This code creates a new, private playlist in the authorized user's channel.
65-
playlists_insert_response = youtube.playlists().insert(
66-
part="snippet,status",
67-
body=dict(
33+
SCOPES = ['https://www.googleapis.com/auth/youtube']
34+
API_SERVICE_NAME = 'youtube'
35+
API_VERSION = 'v3'
36+
37+
# Authorize the request and store authorization credentials.
38+
def get_authenticated_service():
39+
flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRETS_FILE, SCOPES)
40+
credentials = flow.run_console()
41+
return build(API_SERVICE_NAME, API_VERSION, credentials = credentials)
42+
43+
def add_playlist(youtube, args):
44+
45+
body = dict(
6846
snippet=dict(
69-
title="Test Playlist",
70-
description="A private playlist created with the YouTube API v3"
47+
title=args.title,
48+
description=args.description
7149
),
7250
status=dict(
73-
privacyStatus="private"
74-
)
75-
)
76-
).execute()
77-
78-
print "New playlist id: %s" % playlists_insert_response["id"]
51+
privacyStatus='private'
52+
)
53+
)
54+
55+
playlists_insert_response = youtube.playlists().insert(
56+
part='snippet,status',
57+
body=body
58+
).execute()
59+
60+
print 'New playlist ID: %s' % playlists_insert_response['id']
61+
62+
if __name__ == '__main__':
63+
64+
parser = argparse.ArgumentParser()
65+
parser.add_argument('--title',
66+
default='Test Playlist',
67+
help='The title of the new playlist.')
68+
parser.add_argument('--description',
69+
default='A private playlist created with the YouTube Data API.',
70+
help='The description of the new playlist.')
71+
72+
args = parser.parse_args()
73+
74+
youtube = get_authenticated_service()
75+
try:
76+
add_playlist(youtube, args)
77+
except HttpError, e:
78+
print 'An HTTP error %d occurred:\n%s' % (e.resp.status, e.content)

0 commit comments

Comments
 (0)