1
1
#!/usr/bin/python
2
2
3
- import httplib2
3
+ # This code sample shows how to add a channel subscription.
4
+ # The default channel-id is for the GoogleDevelopers YouTube channel.
5
+ # Sample usage:
6
+ # python add_subscription.py --channel-id=UC_x5XG1OV2P6uZZ5FSM9Ttw
7
+
8
+ import argparse
4
9
import os
5
- import sys
10
+ import re
6
11
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
+ import google . oauth2 . credentials
13
+ import google_auth_oauthlib . flow
14
+ from googleapiclient . discovery import build
15
+ from googleapiclient . errors import HttpError
16
+ from google_auth_oauthlib . flow import InstalledAppFlow
12
17
13
18
14
19
# The CLIENT_SECRETS_FILE variable specifies the name of a file that contains
21
26
# https://developers.google.com/youtube/v3/guides/authentication
22
27
# For more information about the client_secrets.json file format, see:
23
28
# https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
24
- CLIENT_SECRETS_FILE = "client_secrets .json"
29
+ CLIENT_SECRETS_FILE = 'client_secret .json'
25
30
26
31
# This OAuth 2.0 access scope allows for full read/write access to the
27
32
# authenticated user's account.
28
- YOUTUBE_READ_WRITE_SCOPE = "https://www.googleapis.com/auth/youtube"
29
- YOUTUBE_API_SERVICE_NAME = "youtube"
30
- YOUTUBE_API_VERSION = "v3"
31
-
32
- # This variable defines a message to display if the CLIENT_SECRETS_FILE is
33
- # missing.
34
- MISSING_CLIENT_SECRETS_MESSAGE = """
35
- WARNING: Please configure OAuth 2.0
36
-
37
- To make this sample run you will need to populate the client_secrets.json file
38
- found at:
39
-
40
- %s
41
-
42
- with information from the {{ Cloud Console }}
43
- {{ https://cloud.google.com/console }}
44
-
45
- For more information about the client_secrets.json file format, please visit:
46
- https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
47
- """ % os .path .abspath (os .path .join (os .path .dirname (__file__ ),
48
- CLIENT_SECRETS_FILE ))
49
-
50
- def get_authenticated_service (args ):
51
- flow = flow_from_clientsecrets (CLIENT_SECRETS_FILE ,
52
- scope = YOUTUBE_READ_WRITE_SCOPE ,
53
- message = MISSING_CLIENT_SECRETS_MESSAGE )
54
-
55
- storage = Storage ("%s-oauth2.json" % sys .argv [0 ])
56
- credentials = storage .get ()
57
-
58
- if credentials is None or credentials .invalid :
59
- credentials = run_flow (flow , storage , args )
60
-
61
- return build (YOUTUBE_API_SERVICE_NAME , YOUTUBE_API_VERSION ,
62
- http = credentials .authorize (httplib2 .Http ()))
33
+ SCOPES = ['https://www.googleapis.com/auth/youtube' ]
34
+ API_SERVICE_NAME = 'youtube'
35
+ API_VERSION = 'v3'
63
36
37
+ def get_authenticated_service ():
38
+ flow = InstalledAppFlow .from_client_secrets_file (CLIENT_SECRETS_FILE , SCOPES )
39
+ credentials = flow .run_console ()
40
+ return build (API_SERVICE_NAME , API_VERSION , credentials = credentials )
64
41
65
42
# This method calls the API's youtube.subscriptions.insert method to add a
66
43
# subscription to the specified channel.
@@ -75,17 +52,18 @@ def add_subscription(youtube, channel_id):
75
52
)
76
53
)).execute ()
77
54
78
- return add_subscription_response [" snippet" ][ " title" ]
55
+ return add_subscription_response [' snippet' ][ ' title' ]
79
56
80
- if __name__ == "__main__" :
81
- argparser .add_argument ("--channel-id" , help = "ID of the channel to subscribe to." ,
82
- default = "UCtVd0c0tGXuTSbU5d8cSBUg" )
83
- args = argparser .parse_args ()
57
+ if __name__ == '__main__' :
58
+ parser = argparse .ArgumentParser (description = 'Process arguments.' )
59
+ parser .add_argument ('--channel-id' , help = 'ID of the channel to subscribe to.' ,
60
+ default = 'UC_x5XG1OV2P6uZZ5FSM9Ttw' )
61
+ args = parser .parse_args ()
84
62
85
- youtube = get_authenticated_service (args )
63
+ youtube = get_authenticated_service ()
86
64
try :
87
65
channel_title = add_subscription (youtube , args .channel_id )
88
66
except HttpError , e :
89
- print " An HTTP error %d occurred:\n %s" % (e .resp .status , e .content )
67
+ print ' An HTTP error %d occurred:\n %s' % (e .resp .status , e .content )
90
68
else :
91
- print " A subscription to '%s' was added." % channel_title
69
+ print ' A subscription to \ ' %s\ ' was added.' % channel_title
0 commit comments