|
1 | 1 | #!/usr/bin/python
|
2 | 2 |
|
3 |
| -import httplib2 |
| 3 | +import argparse |
4 | 4 | import os
|
5 |
| -import sys |
| 5 | +import re |
6 | 6 |
|
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 |
| 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 |
12 | 12 |
|
13 | 13 |
|
14 | 14 | # The CLIENT_SECRETS_FILE variable specifies the name of a file that contains
|
|
21 | 21 | # https://developers.google.com/youtube/v3/guides/authentication
|
22 | 22 | # For more information about the client_secrets.json file format, see:
|
23 | 23 | # https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
|
24 |
| -CLIENT_SECRETS_FILE = "client_secrets.json" |
| 24 | +CLIENT_SECRETS_FILE = 'client_secret.json' |
25 | 25 |
|
26 | 26 | # This OAuth 2.0 access scope allows for read-only access to the authenticated
|
27 | 27 | # user's account, but not other types of account access.
|
28 |
| -YOUTUBE_READONLY_SCOPE = "https://www.googleapis.com/auth/youtube.readonly" |
29 |
| -YOUTUBE_API_SERVICE_NAME = "youtube" |
30 |
| -YOUTUBE_API_VERSION = "v3" |
| 28 | +SCOPES = ['https://www.googleapis.com/auth/youtube.readonly'] |
| 29 | +API_SERVICE_NAME = 'youtube' |
| 30 | +API_VERSION = 'v3' |
31 | 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 |
| 32 | +VALID_BROADCAST_STATUSES = ('all', 'active', 'completed', 'upcoming',) |
36 | 33 |
|
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 |
| -VALID_BROADCAST_STATUSES = ("all", "active", "completed", "upcoming",) |
51 |
| - |
52 |
| -def get_authenticated_service(args): |
53 |
| - flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE, |
54 |
| - scope=YOUTUBE_READONLY_SCOPE, |
55 |
| - message=MISSING_CLIENT_SECRETS_MESSAGE) |
56 |
| - |
57 |
| - storage = Storage("%s-oauth2.json" % sys.argv[0]) |
58 |
| - credentials = storage.get() |
59 |
| - |
60 |
| - if credentials is None or credentials.invalid: |
61 |
| - credentials = run_flow(flow, storage, args) |
62 |
| - |
63 |
| - return build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, |
64 |
| - http=credentials.authorize(httplib2.Http())) |
| 34 | +# Authorize the request and store authorization credentials. |
| 35 | +def get_authenticated_service(): |
| 36 | + flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRETS_FILE, SCOPES) |
| 37 | + credentials = flow.run_console() |
| 38 | + return build(API_SERVICE_NAME, API_VERSION, credentials = credentials) |
65 | 39 |
|
66 | 40 | # Retrieve a list of broadcasts with the specified status.
|
67 | 41 | def list_broadcasts(youtube, broadcast_status):
|
68 |
| - print "Broadcasts with status '%s':" % broadcast_status |
| 42 | + print 'Broadcasts with status "%s":' % broadcast_status |
69 | 43 |
|
70 | 44 | list_broadcasts_request = youtube.liveBroadcasts().list(
|
71 | 45 | broadcastStatus=broadcast_status,
|
72 |
| - part="id,snippet", |
| 46 | + part='id,snippet', |
73 | 47 | maxResults=50
|
74 | 48 | )
|
75 | 49 |
|
76 | 50 | while list_broadcasts_request:
|
77 | 51 | list_broadcasts_response = list_broadcasts_request.execute()
|
78 | 52 |
|
79 |
| - for broadcast in list_broadcasts_response.get("items", []): |
80 |
| - print "%s (%s)" % (broadcast["snippet"]["title"], broadcast["id"]) |
| 53 | + for broadcast in list_broadcasts_response.get('items', []): |
| 54 | + print '%s (%s)' % (broadcast['snippet']['title'], broadcast['id']) |
81 | 55 |
|
82 | 56 | list_broadcasts_request = youtube.liveBroadcasts().list_next(
|
83 | 57 | list_broadcasts_request, list_broadcasts_response)
|
84 | 58 |
|
85 |
| -if __name__ == "__main__": |
86 |
| - argparser.add_argument("--broadcast-status", help="Broadcast status", |
| 59 | +if __name__ == '__main__': |
| 60 | + parser = argparse.ArgumentParser() |
| 61 | + parser.add_argument('--broadcast-status', help='Broadcast status', |
87 | 62 | choices=VALID_BROADCAST_STATUSES, default=VALID_BROADCAST_STATUSES[0])
|
88 |
| - args = argparser.parse_args() |
| 63 | + args = parser.parse_args() |
89 | 64 |
|
90 |
| - youtube = get_authenticated_service(args) |
| 65 | + youtube = get_authenticated_service() |
91 | 66 | try:
|
92 | 67 | list_broadcasts(youtube, args.broadcast_status)
|
93 | 68 | except HttpError, e:
|
94 |
| - print "An HTTP error %d occurred:\n%s" % (e.resp.status, e.content) |
| 69 | + print 'An HTTP error %d occurred:\n%s' % (e.resp.status, e.content) |
0 commit comments