|
1 | 1 | # Sample Python code for user authorization |
2 | 2 |
|
3 | | -import httplib2 |
4 | 3 | import os |
5 | | -import sys |
6 | 4 |
|
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 |
| 5 | +import google.oauth2.credentials |
| 6 | + |
| 7 | +from googleapiclient.discovery import build |
| 8 | +from googleapiclient.errors import HttpError |
| 9 | +from google_auth_oauthlib.flow import InstalledAppFlow |
12 | 10 |
|
13 | 11 | # The CLIENT_SECRETS_FILE variable specifies the name of a file that contains |
14 | 12 | # the OAuth 2.0 information for this application, including its client_id and |
|
17 | 15 |
|
18 | 16 | # This OAuth 2.0 access scope allows for full read/write access to the |
19 | 17 | # authenticated user's account and requires requests to use an SSL connection. |
20 | | -YOUTUBE_READ_WRITE_SSL_SCOPE = "https://www.googleapis.com/auth/youtube.readonly" |
21 | | -API_SERVICE_NAME = "youtube" |
22 | | -API_VERSION = "v3" |
23 | | - |
24 | | -# This variable defines a message to display if the CLIENT_SECRETS_FILE is |
25 | | -# missing. |
26 | | -MISSING_CLIENT_SECRETS_MESSAGE = "WARNING: Please configure OAuth 2.0" |
27 | | - |
28 | | -# Authorize the request and store authorization credentials. |
29 | | -def get_authenticated_service(args): |
30 | | - flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE, scope=YOUTUBE_READ_WRITE_SSL_SCOPE, |
31 | | - message=MISSING_CLIENT_SECRETS_MESSAGE) |
32 | | - |
33 | | - storage = Storage("%s-oauth2.json" % sys.argv[0]) |
34 | | - credentials = storage.get() |
| 18 | +SCOPES = ['https://www.googleapis.com/auth/youtube.force-ssl'] |
| 19 | +API_SERVICE_NAME = 'youtube' |
| 20 | +API_VERSION = 'v3' |
35 | 21 |
|
36 | | - if credentials is None or credentials.invalid: |
37 | | - credentials = run_flow(flow, storage, args) |
| 22 | +def get_authenticated_service(): |
| 23 | + flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRETS_FILE, SCOPES) |
38 | 24 |
|
39 | | - # Trusted testers can download this discovery document from the developers page |
40 | | - # and it should be in the same directory with the code. |
41 | | - return build(API_SERVICE_NAME, API_VERSION, |
42 | | - http=credentials.authorize(httplib2.Http())) |
| 25 | + credentials = flow.run_console() |
43 | 26 |
|
44 | | -args = argparser.parse_args() |
45 | | -service = get_authenticated_service(args) |
46 | | - |
47 | | -### END BOILERPLATE CODE |
48 | | - |
49 | | -# Sample python code for channels.list |
| 27 | + return build(API_SERVICE_NAME, API_VERSION, credentials = credentials) |
50 | 28 |
|
51 | 29 | def channels_list_by_username(service, **kwargs): |
52 | 30 | results = service.channels().list( |
53 | 31 | **kwargs |
54 | 32 | ).execute() |
55 | | - |
| 33 | + |
56 | 34 | print('This channel\'s ID is %s. Its title is %s, and it has %s views.' % |
57 | 35 | (results['items'][0]['id'], |
58 | 36 | results['items'][0]['snippet']['title'], |
59 | 37 | results['items'][0]['statistics']['viewCount'])) |
60 | 38 |
|
61 | | -channels_list_by_username(service, part='snippet,contentDetails,statistics', forUsername='GoogleDevelopers') |
| 39 | +if __name__ == '__main__': |
| 40 | + # When running locally, disable OAuthlib's HTTPs verification. When |
| 41 | + # running in production *do not* leave this option enabled. |
| 42 | + os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1' |
| 43 | + #app.run('localhost', 8090, debug=True) |
| 44 | + service = get_authenticated_service() |
| 45 | + channels_list_by_username(service, |
| 46 | + part='snippet,contentDetails,statistics', |
| 47 | + forUsername='GoogleDevelopers') |
0 commit comments