Skip to content

Commit d788e74

Browse files
Update quickstart.py auth libraries
Update auth library from oauth2client to google-auth.
1 parent 5b036f3 commit d788e74

File tree

1 file changed

+22
-36
lines changed

1 file changed

+22
-36
lines changed

python/quickstart.py

Lines changed: 22 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
# Sample Python code for user authorization
22

3-
import httplib2
43
import os
5-
import sys
64

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
1210

1311
# The CLIENT_SECRETS_FILE variable specifies the name of a file that contains
1412
# the OAuth 2.0 information for this application, including its client_id and
@@ -17,45 +15,33 @@
1715

1816
# This OAuth 2.0 access scope allows for full read/write access to the
1917
# 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'
3521

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)
3824

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()
4326

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)
5028

5129
def channels_list_by_username(service, **kwargs):
5230
results = service.channels().list(
5331
**kwargs
5432
).execute()
55-
33+
5634
print('This channel\'s ID is %s. Its title is %s, and it has %s views.' %
5735
(results['items'][0]['id'],
5836
results['items'][0]['snippet']['title'],
5937
results['items'][0]['statistics']['viewCount']))
6038

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

Comments
 (0)