Skip to content

Commit 7a98f30

Browse files
"Added sample: java/src/main/java/com/google/api/services/samples/youtube/cmdline/data/InvideoProgramming.java"
1 parent a356516 commit 7a98f30

1 file changed

Lines changed: 188 additions & 0 deletions

File tree

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
/*
2+
* Copyright (c) 2013 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
15+
package com.google.api.services.samples.youtube.cmdline.data;
16+
17+
import com.google.api.client.auth.oauth2.Credential;
18+
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
19+
import com.google.api.client.http.InputStreamContent;
20+
import com.google.api.services.samples.youtube.cmdline.Auth;
21+
import com.google.api.services.youtube.YouTube;
22+
import com.google.api.services.youtube.model.*;
23+
import com.google.common.collect.Lists;
24+
25+
import java.io.IOException;
26+
import java.math.BigInteger;
27+
import java.util.List;
28+
29+
/**
30+
* Add a featured video to a channel.
31+
*
32+
* @author Ikai Lan <ikai@google.com>
33+
*/
34+
public class InvideoProgramming {
35+
36+
/**
37+
* Define a global instance of a Youtube object, which will be used
38+
* to make YouTube Data API requests.
39+
*/
40+
private static YouTube youtube;
41+
42+
/**
43+
* This sample video introduces WebM on YouTube Developers Live.
44+
*/
45+
private static final String FEATURED_VIDEO_ID = "w4eiUiauo2w";
46+
47+
/**
48+
* This code sample demonstrates different ways that the API can be used to
49+
* promote your channel content. It includes code for the following tasks:
50+
* <ol>
51+
* <li>Feature a video.</li>
52+
* <li>Feature a link to a social media channel.</li>
53+
* <li>Set a watermark for videos on your channel.</li>
54+
* </ol>
55+
*
56+
* @param args command line args (not used).
57+
*/
58+
public static void main(String[] args) {
59+
60+
// This OAuth 2.0 access scope allows for full read/write access to the
61+
// authenticated user's account.
62+
List<String> scopes = Lists.newArrayList("https://www.googleapis.com/auth/youtube");
63+
64+
try {
65+
// Authorize the request.
66+
Credential credential = Auth.authorize(scopes, "invideoprogramming");
67+
68+
// This object is used to make YouTube Data API requests.
69+
youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential)
70+
.setApplicationName("youtube-cmdline-invideoprogramming-sample")
71+
.build();
72+
73+
// Construct a request to retrieve the current user's channel ID.
74+
// In the API response, only include channel information needed for
75+
// this use case. The channel's uploads playlist identifies the
76+
// channel's most recently uploaded video.
77+
// See https://developers.google.com/youtube/v3/docs/channels/list
78+
ChannelListResponse channelListResponse = youtube.channels().list("id,contentDetails")
79+
.setMine(true)
80+
.setFields("items(contentDetails/relatedPlaylists/uploads,id)")
81+
.execute();
82+
83+
// The user's default channel is the first item in the list. If the
84+
// user does not have a channel, this code should throw a
85+
// GoogleJsonResponseException explaining the issue.
86+
Channel myChannel = channelListResponse.getItems().get(0);
87+
String channelId = myChannel.getId();
88+
89+
// The promotion appears 15000ms (15 seconds) before the video ends.
90+
InvideoTiming invideoTiming = new InvideoTiming();
91+
invideoTiming.setOffsetMs(BigInteger.valueOf(15000l));
92+
invideoTiming.setType("offsetFromEnd");
93+
94+
// This is one type of promotion and promotes a video.
95+
PromotedItemId promotedItemId = new PromotedItemId();
96+
promotedItemId.setType("video");
97+
promotedItemId.setVideoId(FEATURED_VIDEO_ID);
98+
99+
// Set a custom message providing additional information about the
100+
// promoted video or your channel.
101+
PromotedItem promotedItem = new PromotedItem();
102+
promotedItem.setCustomMessage("Check out this video about WebM!");
103+
promotedItem.setId(promotedItemId);
104+
105+
// Construct an object representing the invideo promotion data, and
106+
// add it to the channel.
107+
InvideoPromotion invideoPromotion = new InvideoPromotion();
108+
invideoPromotion.setDefaultTiming(invideoTiming);
109+
invideoPromotion.setItems(Lists.newArrayList(promotedItem));
110+
111+
Channel channel = new Channel();
112+
channel.setId(channelId);
113+
channel.setInvideoPromotion(invideoPromotion);
114+
115+
Channel updateChannelResponse = youtube.channels()
116+
.update("invideoPromotion", channel)
117+
.execute();
118+
119+
// Print data from the API response.
120+
System.out.println("\n================== Updated Channel Information ==================\n");
121+
System.out.println("\t- Channel ID: " + updateChannelResponse.getId());
122+
123+
InvideoPromotion promotions = updateChannelResponse.getInvideoPromotion();
124+
promotedItem = promotions.getItems().get(0); // We only care about the first item
125+
System.out.println("\t- Invideo promotion video ID: " + promotedItem
126+
.getId()
127+
.getVideoId());
128+
System.out.println("\t- Promotion message: " + promotedItem.getCustomMessage());
129+
130+
// In-video programming can also be used to feature links to
131+
// associated websites, merchant sites, or social networking sites.
132+
// The code below overrides the promotional video set above by
133+
// featuring a link to the YouTube Developers Twitter feed.
134+
PromotedItemId promotedTwitterFeed = new PromotedItemId();
135+
promotedTwitterFeed.setType("website");
136+
promotedTwitterFeed.setWebsiteUrl("https://twitter.com/youtubedev");
137+
138+
promotedItem = new PromotedItem();
139+
promotedItem.setCustomMessage("Follow us on Twitter!");
140+
promotedItem.setId(promotedTwitterFeed);
141+
142+
invideoPromotion.setItems(Lists.newArrayList(promotedItem));
143+
channel.setInvideoPromotion(invideoPromotion);
144+
145+
// Call the API to set the in-video promotion data.
146+
updateChannelResponse = youtube.channels()
147+
.update("invideoPromotion", channel)
148+
.execute();
149+
150+
// Print data from the API response.
151+
System.out.println("\n================== Updated Channel Information ==================\n");
152+
System.out.println("\t- Channel ID: " + updateChannelResponse.getId());
153+
154+
promotions = updateChannelResponse.getInvideoPromotion();
155+
promotedItem = promotions.getItems().get(0);
156+
System.out.println("\t- Invideo promotion URL: " + promotedItem
157+
.getId()
158+
.getWebsiteUrl());
159+
System.out.println("\t- Promotion message: " + promotedItem.getCustomMessage();
160+
161+
// This example sets a custom watermark for the channel. The image
162+
// used is the watermark.jpg file in the "resources/" directory.
163+
InputStreamContent mediaContent = new InputStreamContent("image/jpeg",
164+
InvideoProgramming.class.getResourceAsStream("/watermark.jpg"));
165+
166+
// Indicate that the watermark should display during the last 15
167+
// seconds of the video.
168+
InvideoTiming watermarkTiming = new InvideoTiming();
169+
watermarkTiming.setType("offsetFromEnd");
170+
watermarkTiming.setDurationMs(BigInteger.valueOf(15000l));
171+
watermarkTiming.setOffsetMs(BigInteger.valueOf(15000l));
172+
173+
InvideoBranding invideoBranding = new InvideoBranding();
174+
invideoBranding.setTiming(watermarkTiming);
175+
youtube.watermarks().set(channelId, invideoBranding, mediaContent).execute();
176+
177+
} catch (GoogleJsonResponseException e) {
178+
System.err.println("GoogleJsonResponseException code: " + e.getDetails().getCode() + " : "
179+
+ e.getDetails().getMessage());
180+
e.printStackTrace();
181+
182+
} catch (IOException e) {
183+
System.err.println("IOException: " + e.getMessage());
184+
e.printStackTrace();
185+
}
186+
}
187+
188+
}

0 commit comments

Comments
 (0)