1
1
#!/usr/bin/python
2
2
3
- import httplib2
4
3
import os
5
4
import random
6
- import sys
7
5
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
13
11
14
12
15
13
# The CLIENT_SECRETS_FILE variable specifies the name of a file that contains
23
21
# For more information about the client_secrets.json file format, see:
24
22
# https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
25
23
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'
45
25
46
26
# This OAuth 2.0 access scope allows for full read/write access to the
47
27
# 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'
51
31
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 )
64
37
65
38
def get_current_channel_sections (youtube ):
66
39
channel_sections_list_response = youtube .channelSections ().list (
67
- part = " snippet,contentDetails" ,
40
+ part = ' snippet,contentDetails' ,
68
41
mine = True
69
42
).execute ()
70
43
71
- return channel_sections_list_response [" items" ]
44
+ return channel_sections_list_response [' items' ]
72
45
73
46
def shuffle_channel_sections (youtube , channel_sections ):
74
47
# This will randomly reorder the items in the channel_sections list.
@@ -77,21 +50,19 @@ def shuffle_channel_sections(youtube, channel_sections):
77
50
for channel_section in channel_sections :
78
51
# Each section in the list of shuffled sections is sequentially
79
52
# set to position 0, i.e. the top.
80
- channel_section [" snippet" ][ " position" ] = 0
53
+ channel_section [' snippet' ][ ' position' ] = 0
81
54
82
55
youtube .channelSections ().update (
83
- part = " snippet,contentDetails" ,
56
+ part = ' snippet,contentDetails' ,
84
57
body = channel_section
85
58
).execute ()
86
59
87
60
if __name__ == '__main__' :
88
- args = argparser .parse_args ()
89
-
90
- youtube = get_authenticated_service (args )
61
+ youtube = get_authenticated_service ()
91
62
try :
92
63
channel_sections = get_current_channel_sections (youtube )
93
64
shuffle_channel_sections (youtube , channel_sections )
94
65
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 )
96
67
else :
97
- print " The existing channel sections have been randomly shuffled."
68
+ print ' The existing channel sections have been randomly shuffled.'
0 commit comments