11#!/usr/bin/python
22
3- import httplib2
43import os
54import random
6- import sys
75
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
6+ import google . oauth2 . credentials
7+ import google_auth_oauthlib . flow
8+ from googleapiclient . discovery import build
9+ from googleapiclient . errors import HttpError
10+ from google_auth_oauthlib . flow import InstalledAppFlow
1311
1412
1513# The CLIENT_SECRETS_FILE variable specifies the name of a file that contains
2321# For more information about the client_secrets.json file format, see:
2422# https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
2523
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 ))
24+ CLIENT_SECRETS_FILE = 'client_secret.json'
4525
4626# This OAuth 2.0 access scope allows for full read/write access to the
4727# authenticated user's account.
48- YOUTUBE_SCOPE = " https://www.googleapis.com/auth/youtube"
49- YOUTUBE_API_SERVICE_NAME = " youtube"
50- YOUTUBE_API_VERSION = "v3"
28+ SCOPES = [ ' https://www.googleapis.com/auth/youtube' ]
29+ API_SERVICE_NAME = ' youtube'
30+ API_VERSION = 'v3'
5131
52- def get_authenticated_service (args ):
53- flow = flow_from_clientsecrets (CLIENT_SECRETS_FILE , scope = YOUTUBE_SCOPE ,
54- message = MISSING_CLIENT_SECRETS_MESSAGE )
55-
56- storage = Storage ("%s-oauth2.json" % sys .argv [0 ])
57- credentials = storage .get ()
58-
59- if credentials is None or credentials .invalid :
60- credentials = run_flow (flow , storage , args )
61-
62- return build (YOUTUBE_API_SERVICE_NAME , YOUTUBE_API_VERSION ,
63- http = credentials .authorize (httplib2 .Http ()))
32+ # Authorize the request and store authorization credentials.
33+ def get_authenticated_service ():
34+ flow = InstalledAppFlow .from_client_secrets_file (CLIENT_SECRETS_FILE , SCOPES )
35+ credentials = flow .run_console ()
36+ return build (API_SERVICE_NAME , API_VERSION , credentials = credentials )
6437
6538def get_current_channel_sections (youtube ):
6639 channel_sections_list_response = youtube .channelSections ().list (
67- part = " snippet,contentDetails" ,
40+ part = ' snippet,contentDetails' ,
6841 mine = True
6942 ).execute ()
7043
71- return channel_sections_list_response [" items" ]
44+ return channel_sections_list_response [' items' ]
7245
7346def shuffle_channel_sections (youtube , channel_sections ):
7447 # This will randomly reorder the items in the channel_sections list.
@@ -77,21 +50,19 @@ def shuffle_channel_sections(youtube, channel_sections):
7750 for channel_section in channel_sections :
7851 # Each section in the list of shuffled sections is sequentially
7952 # set to position 0, i.e. the top.
80- channel_section [" snippet" ][ " position" ] = 0
53+ channel_section [' snippet' ][ ' position' ] = 0
8154
8255 youtube .channelSections ().update (
83- part = " snippet,contentDetails" ,
56+ part = ' snippet,contentDetails' ,
8457 body = channel_section
8558 ).execute ()
8659
8760if __name__ == '__main__' :
88- args = argparser .parse_args ()
89-
90- youtube = get_authenticated_service (args )
61+ youtube = get_authenticated_service ()
9162 try :
9263 channel_sections = get_current_channel_sections (youtube )
9364 shuffle_channel_sections (youtube , channel_sections )
9465 except HttpError , e :
95- print " An HTTP error %d occurred:\n %s" % (e .resp .status , e .content )
66+ print ' An HTTP error %d occurred:\n %s' % (e .resp .status , e .content )
9667 else :
97- print " The existing channel sections have been randomly shuffled."
68+ print ' The existing channel sections have been randomly shuffled.'
0 commit comments