|
1 | 1 | #!/usr/bin/python
|
2 | 2 |
|
3 |
| -import httplib2 |
4 | 3 | import os
|
5 |
| -import sys |
| 4 | +import re |
6 | 5 |
|
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 |
| 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 |
12 | 11 |
|
13 | 12 |
|
14 | 13 | # The CLIENT_SECRETS_FILE variable specifies the name of a file that contains
|
|
21 | 20 | # https://developers.google.com/youtube/v3/guides/authentication
|
22 | 21 | # For more information about the client_secrets.json file format, see:
|
23 | 22 | # https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
|
24 |
| -CLIENT_SECRETS_FILE = "client_secrets.json" |
| 23 | +CLIENT_SECRETS_FILE = 'client_secret.json' |
25 | 24 |
|
26 | 25 | # This OAuth 2.0 access scope allows for read-only access to the authenticated
|
27 | 26 | # 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" |
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_READONLY_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())) |
63 |
| - |
| 27 | +SCOPES = ['https://www.googleapis.com/auth/youtube.readonly'] |
| 28 | +API_SERVICE_NAME = 'youtube' |
| 29 | +API_VERSION = 'v3' |
| 30 | + |
| 31 | +# Authorize the request and store authorization credentials. |
| 32 | +def get_authenticated_service(): |
| 33 | + flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRETS_FILE, SCOPES) |
| 34 | + credentials = flow.run_console() |
| 35 | + return build(API_SERVICE_NAME, API_VERSION, credentials = credentials) |
| 36 | + |
64 | 37 | # Retrieve a list of the liveStream resources associated with the currently
|
65 | 38 | # authenticated user's channel.
|
66 | 39 | def list_streams(youtube):
|
67 |
| - print "Live streams:" |
| 40 | + print 'Live streams:' |
68 | 41 |
|
69 | 42 | list_streams_request = youtube.liveStreams().list(
|
70 |
| - part="id,snippet", |
| 43 | + part='id,snippet', |
71 | 44 | mine=True,
|
72 | 45 | maxResults=50
|
73 |
| - ) |
74 |
| - |
| 46 | + ) |
| 47 | + |
75 | 48 | while list_streams_request:
|
76 | 49 | list_streams_response = list_streams_request.execute()
|
77 |
| - |
78 |
| - for stream in list_streams_response.get("items", []): |
79 |
| - print "%s (%s)" % (stream["snippet"]["title"], stream["id"]) |
80 |
| - |
| 50 | + |
| 51 | + for stream in list_streams_response.get('items', []): |
| 52 | + print '%s (%s)' % (stream['snippet']['title'], stream['id']) |
| 53 | + |
81 | 54 | list_streams_request = youtube.liveStreams().list_next(
|
82 | 55 | list_streams_request, list_streams_response)
|
83 |
| - |
84 |
| -if __name__ == "__main__": |
85 |
| - args = argparser.parse_args() |
86 |
| - |
87 |
| - youtube = get_authenticated_service(args) |
| 56 | + |
| 57 | +if __name__ == '__main__': |
| 58 | + youtube = get_authenticated_service() |
88 | 59 | try:
|
89 | 60 | list_streams(youtube)
|
90 | 61 | except HttpError, e:
|
91 |
| - print "An HTTP error %d occurred:\n%s" % (e.resp.status, e.content) |
| 62 | + print 'An HTTP error %d occurred:\n%s' % (e.resp.status, e.content) |
0 commit comments