Skip to content

Commit afdf673

Browse files
"Added sample: python/channel_bulletin.py"
1 parent e1213d2 commit afdf673

1 file changed

Lines changed: 118 additions & 0 deletions

File tree

python/channel_bulletin.py

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#!/usr/bin/python
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+
14+
# The CLIENT_SECRETS_FILE variable specifies the name of a file that contains
15+
# the OAuth 2.0 information for this application, including its client_id and
16+
# client_secret. You can acquire an OAuth 2.0 client ID and client secret from
17+
# the {{ Google Cloud Console }} at
18+
# {{ https://cloud.google.com/console }}.
19+
# Please ensure that you have enabled the YouTube Data API for your project.
20+
# For more information about using OAuth2 to access the YouTube Data API, see:
21+
# https://developers.google.com/youtube/v3/guides/authentication
22+
# For more information about the client_secrets.json file format, see:
23+
# https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
24+
CLIENT_SECRETS_FILE = "client_secrets.json"
25+
26+
# This OAuth 2.0 access scope allows for full read/write access to the
27+
# authenticated user's account.
28+
YOUTUBE_READ_WRITE_SCOPE = "https://www.googleapis.com/auth/youtube"
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_READ_WRITE_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+
64+
# This method calls the API's youtube.activities.insert method to post the
65+
# channel bulletin.
66+
def post_bulletin(youtube, args):
67+
body = dict(
68+
snippet=dict(
69+
description=args.message
70+
)
71+
)
72+
73+
if args.video_id:
74+
body["contentDetails"] = dict(
75+
bulletin=dict(
76+
resourceId=dict(
77+
kind="youtube#video",
78+
videoId=args.video_id
79+
)
80+
)
81+
)
82+
83+
if args.playlist_id:
84+
body["contentDetails"] = dict(
85+
bulletin=dict(
86+
resourceId=dict(
87+
kind="youtube#playlist",
88+
playlistId=args.playlist_id
89+
)
90+
)
91+
)
92+
93+
youtube.activities().insert(
94+
part=",".join(body.keys()),
95+
body=body
96+
).execute()
97+
98+
if __name__ == "__main__":
99+
argparser.add_argument("--message", required=True,
100+
help="Text of message to post.")
101+
argparser.add_argument("--video-id",
102+
help="Optional ID of video to post.")
103+
argparser.add_argument("--playlist-id",
104+
help="Optional ID of playlist to post.")
105+
args = argparser.parse_args()
106+
107+
# You can post a message with or without an accompanying video or playlist.
108+
# However, you can't post a video and a playlist at the same time.
109+
if args.video_id and args.playlist_id:
110+
exit("You cannot post a video and a playlist at the same time.")
111+
112+
youtube = get_authenticated_service(args)
113+
try:
114+
post_bulletin(youtube, args)
115+
except HttpError, e:
116+
print "An HTTP error %d occurred:\n%s" % (e.resp.status, e.content)
117+
else:
118+
print "The bulletin was posted to your channel."

0 commit comments

Comments
 (0)