Skip to content

Commit 294e20f

Browse files
committed
"Added sample: python/unset_watermark.py"
1 parent 104c2fe commit 294e20f

1 file changed

Lines changed: 88 additions & 0 deletions

File tree

python/unset_watermark.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#!/usr/bin/python
2+
3+
# Usage example:
4+
# python unset_watermark.py --channelId='<channel_id>' --unset
5+
6+
import json
7+
import os
8+
import sys
9+
import httplib2
10+
11+
from apiclient.discovery import build
12+
from apiclient.errors import HttpError
13+
from oauth2client.file import Storage
14+
from oauth2client.client import flow_from_clientsecrets
15+
from oauth2client.tools import run
16+
from optparse import OptionParser
17+
18+
19+
# CLIENT_SECRETS_FILE, name of a file containing the OAuth 2.0 information for
20+
# this application, including client_id and client_secret. You can acquire an
21+
# ID/secret pair from the API Access tab on the Google APIs Console
22+
# http://code.google.com/apis/console#access
23+
# For more information about using OAuth2 to access Google APIs, please visit:
24+
# https://developers.google.com/accounts/docs/OAuth2
25+
# For more information about the client_secrets.json file format, please visit:
26+
# https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
27+
# Please ensure that you have enabled the YouTube Data API for your project.
28+
CLIENT_SECRETS_FILE = "client_secrets.json"
29+
30+
# An OAuth 2 access scope that allows for full read/write access.
31+
YOUTUBE_READ_WRITE_SCOPE = "https://www.googleapis.com/auth/youtube"
32+
YOUTUBE_API_SERVICE_NAME = "youtube"
33+
YOUTUBE_API_VERSION = "v3"
34+
35+
# Helpful message to display if the CLIENT_SECRETS_FILE is missing.
36+
MISSING_CLIENT_SECRETS_MESSAGE = """
37+
WARNING: Please configure OAuth 2.0
38+
39+
To make this sample run you will need to populate the client_secrets.json file
40+
found at:
41+
%s
42+
with information from the APIs Console
43+
https://developers.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+
# Authorize the request and store authorization credentials.
51+
def get_authenticated_service():
52+
flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE,
53+
scope=YOUTUBE_READ_WRITE_SCOPE,
54+
message=MISSING_CLIENT_SECRETS_MESSAGE)
55+
56+
storage = Storage("%s-oauth2.json" % sys.argv[0])
57+
credentials = storage.get()
58+
59+
if credentials is None or credentials.invalid:
60+
credentials = run(flow, storage)
61+
62+
return build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
63+
http=credentials.authorize(httplib2.Http()))
64+
65+
66+
# Call the API's watermarks.unset method to remove the watermark image.
67+
def unset_watermark(youtube, channel_id):
68+
try:
69+
youtube.watermarks().unset(channelId=channel_id).execute()
70+
except HttpError as e:
71+
print "Error while unsetting watermark: %s" % e.content
72+
raise e
73+
74+
75+
if __name__ == "__main__":
76+
parser = OptionParser()
77+
parser.add_option("--channelid", dest="channelid",
78+
help="Required; id of channel whose watermark you're updating.")
79+
(options, args) = parser.parse_args()
80+
81+
if not options.channelid:
82+
parser.print_help()
83+
exit()
84+
85+
youtube = get_authenticated_service()
86+
87+
unset_watermark(youtube, options.channelid)
88+
print "The watermark was successfully unset."

0 commit comments

Comments
 (0)