Skip to content

Commit c03017d

Browse files
Update auth libraries used in example
The oauth2client library is deprecated. This update changes the sample to use the google-auth and google-auth-oauthlib libraries instead.
1 parent fe3b13a commit c03017d

File tree

1 file changed

+50
-70
lines changed

1 file changed

+50
-70
lines changed

python/add_channel_section.py

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

3-
import httplib2
3+
import argparse
44
import os
55
import re
6-
import sys
76

8-
from apiclient.discovery import build
9-
from apiclient.errors import HttpError
10-
from oauth2client.client import flow_from_clientsecrets
11-
from oauth2client.file import Storage
12-
from oauth2client.tools import argparser, run_flow
7+
import google.oauth2.credentials
8+
import google_auth_oauthlib.flow
9+
from googleapiclient.discovery import build
10+
from googleapiclient.errors import HttpError
11+
from google_auth_oauthlib.flow import InstalledAppFlow
1312

1413

1514
# The CLIENT_SECRETS_FILE variable specifies the name of a file that contains
@@ -23,72 +22,50 @@
2322
# For more information about the client_secrets.json file format, see:
2423
# https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
2524

26-
CLIENT_SECRETS_FILE = "client_secrets.json"
27-
28-
# This variable defines a message to display if the CLIENT_SECRETS_FILE is
29-
# missing.
30-
MISSING_CLIENT_SECRETS_MESSAGE = """
31-
WARNING: Please configure OAuth 2.0
32-
33-
To make this sample run you will need to populate the client_secrets.json file
34-
found at:
35-
36-
%s
37-
38-
with information from the {{ Cloud Console }}
39-
{{ https://cloud.google.com/console }}
40-
41-
For more information about the client_secrets.json file format, please visit:
42-
https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
43-
""" % os.path.abspath(os.path.join(os.path.dirname(__file__),
44-
CLIENT_SECRETS_FILE))
25+
CLIENT_SECRETS_FILE = 'client_secret.json'
4526

4627
# This OAuth 2.0 access scope allows for full read/write access to the
4728
# authenticated user's account.
48-
YOUTUBE_SCOPE = "https://www.googleapis.com/auth/youtube"
49-
YOUTUBE_API_SERVICE_NAME = "youtube"
50-
YOUTUBE_API_VERSION = "v3"
51-
52-
SECTION_TYPES = ("allPlaylists", "completedEvents", "likedPlaylists",
53-
"likes", "liveEvents", "multipleChannels", "multiplePlaylists",
54-
"popularUploads", "recentActivity", "recentPosts", "recentUploads",
55-
"singlePlaylist", "upcomingEvents",)
56-
SECTION_STYLES = ("horizontalRow", "verticalList",)
29+
SCOPES = ['https://www.googleapis.com/auth/youtube']
30+
API_SERVICE_NAME = 'youtube'
31+
API_VERSION = 'v3'
5732

58-
def get_authenticated_service(args):
59-
flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE, scope=YOUTUBE_SCOPE,
60-
message=MISSING_CLIENT_SECRETS_MESSAGE)
33+
SECTION_TYPES = ('allPlaylists', 'completedEvents', 'likedPlaylists',
34+
'likes', 'liveEvents', 'multipleChannels', 'multiplePlaylists',
35+
'popularUploads', 'recentActivity', 'recentPosts', 'recentUploads',
36+
'singlePlaylist', 'upcomingEvents',)
37+
SECTION_STYLES = ('horizontalRow', 'verticalList',)
6138

62-
storage = Storage("%s-oauth2.json" % sys.argv[0])
63-
credentials = storage.get()
6439

65-
if credentials is None or credentials.invalid:
66-
credentials = run_flow(flow, storage, args)
40+
def get_authenticated_service():
41+
flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRETS_FILE, SCOPES)
42+
credentials = flow.run_console()
43+
return build(API_SERVICE_NAME, API_VERSION, credentials = credentials)
6744

68-
return build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
69-
http=credentials.authorize(httplib2.Http()))
45+
def print_response(response):
46+
print(response)
7047

7148
def enable_browse_view(youtube):
7249
channels_list_response = youtube.channels().list(
73-
part="brandingSettings",
50+
part='brandingSettings',
7451
mine=True
7552
).execute()
7653

77-
channel = channels_list_response["items"][0]
78-
channel["brandingSettings"]["channel"]["showBrowseView"] = True
54+
channel = channels_list_response['items'][0]
55+
channel['brandingSettings']['channel']['showBrowseView'] = True
7956

8057
youtube.channels().update(
81-
part="brandingSettings",
58+
part='brandingSettings',
8259
body=channel
8360
).execute()
8461

8562
def add_channel_section(youtube, args):
8663
channels = None
8764
if args.channels:
88-
channels = re.split("\s*,\s*", args.channels)
65+
channels = re.split('\s*,\s*', args.channels)
8966
playlists = None
9067
if args.playlists:
91-
playlists = re.split("\s*,\s*", args.playlists)
68+
playlists = re.split('\s*,\s*', args.playlists)
9269

9370
body = dict(
9471
snippet=dict(
@@ -104,28 +81,31 @@ def add_channel_section(youtube, args):
10481
)
10582

10683
youtube.channelSections().insert(
107-
part="snippet,contentDetails",
84+
part='snippet,contentDetails',
10885
body=body
10986
).execute()
11087

11188
if __name__ == '__main__':
112-
argparser.add_argument("--type", choices=SECTION_TYPES, required=True,
113-
help="The type of the section to be added.")
114-
argparser.add_argument("--style", choices=SECTION_STYLES, required=True,
115-
help="The style of the section to be added.")
116-
argparser.add_argument("--title",
117-
help=("The title to display for the new section. This is only used "
118-
"with the multiplePlaylists or multipleChannels section types."))
119-
argparser.add_argument("--position", type=int,
120-
help=("The position of the new section. "
121-
"Use 0 for the top, or don't set a value for the bottom."))
122-
argparser.add_argument("--playlists",
123-
help="One or more playlist ids, comma-separated (e.g. PL...).")
124-
argparser.add_argument("--channels",
125-
help="One or more channel ids, comma-separated (e.g. UC...).")
126-
args = argparser.parse_args()
127-
128-
youtube = get_authenticated_service(args)
89+
90+
parser = argparse.ArgumentParser(description='Process some integers.')
91+
parser.add_argument('--type', choices=SECTION_TYPES, required=True,
92+
help='The type of the section to be added.')
93+
parser.add_argument('--style', choices=SECTION_STYLES, required=True,
94+
help='The style of the section to be added.')
95+
parser.add_argument('--title',
96+
help='The title to display for the new section. This is only used '
97+
'with the multiplePlaylists or multipleChannels section types.')
98+
parser.add_argument('--position', type=int,
99+
help='The position of the new section. Use 0 for the top, '
100+
'or don\'t set a value for the bottom.')
101+
parser.add_argument('--playlists',
102+
help='One or more playlist ids, comma-separated (e.g. PL...).')
103+
parser.add_argument('--channels',
104+
help='One or more channel ids, comma-separated (e.g. UC...).')
105+
106+
args = parser.parse_args()
107+
108+
youtube = get_authenticated_service()
129109
try:
130110
# Before channel shelves will appear on your channel's web page, browse
131111
# view needs to be enabled. If you know that your channel already has
@@ -135,6 +115,6 @@ def add_channel_section(youtube, args):
135115

136116
add_channel_section(youtube, args)
137117
except HttpError, e:
138-
print "An HTTP error %d occurred:\n%s" % (e.resp.status, e.content)
118+
print 'An HTTP error %d occurred:\n%s' % (e.resp.status, e.content)
139119
else:
140-
print "Added new channel section."
120+
print 'Added new channel section.

0 commit comments

Comments
 (0)