Skip to content
This repository was archived by the owner on Jan 18, 2026. It is now read-only.

Commit 55bf401

Browse files
Add file for coming-soon Python quickstart guide.
1 parent f37fb9f commit 55bf401

1 file changed

Lines changed: 61 additions & 0 deletions

File tree

python/quickstart.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Sample Python code for user authorization
2+
3+
import httplib2
4+
import os
5+
import sys
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
12+
13+
# The CLIENT_SECRETS_FILE variable specifies the name of a file that contains
14+
# the OAuth 2.0 information for this application, including its client_id and
15+
# client_secret.
16+
CLIENT_SECRETS_FILE = "client_secret.json"
17+
18+
# This OAuth 2.0 access scope allows for full read/write access to the
19+
# 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()
35+
36+
if credentials is None or credentials.invalid:
37+
credentials = run_flow(flow, storage, args)
38+
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()))
43+
44+
args = argparser.parse_args()
45+
service = get_authenticated_service(args)
46+
47+
### END BOILERPLATE CODE
48+
49+
# Sample python code for channels.list
50+
51+
def channels_list_by_username(service, **kwargs):
52+
results = service.channels().list(
53+
**kwargs
54+
).execute()
55+
56+
print('This channel\'s ID is %s. Its title is %s, and it has %s views.' %
57+
(results['items'][0]['id'],
58+
results['items'][0]['snippet']['title'],
59+
results['items'][0]['statistics']['viewCount']))
60+
61+
channels_list_by_username(service, part='snippet,contentDetails,statistics', forUsername='GoogleDevelopers')

0 commit comments

Comments
 (0)