Skip to content

Commit 8c6cbe1

Browse files
"Added sample: java/src/main/java/com/google/api/services/samples/youtube/cmdline/data/ChannelBulletin.java"
1 parent eb02301 commit 8c6cbe1

1 file changed

Lines changed: 145 additions & 0 deletions

File tree

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/*
2+
* Copyright (c) 2012 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.services.samples.youtube.cmdline.Auth;
20+
import com.google.api.services.youtube.YouTube;
21+
import com.google.api.services.youtube.model.*;
22+
import com.google.common.collect.Lists;
23+
24+
import java.util.Calendar;
25+
import java.util.List;
26+
27+
/**
28+
* Create a video bulletin that is posted to the user's channel feed.
29+
*
30+
* @author Jeremy Walker
31+
*/
32+
public class ChannelBulletin {
33+
34+
/**
35+
* Define a global instance of a Youtube object, which will be used
36+
* to make YouTube Data API requests.
37+
*/
38+
private static YouTube youtube;
39+
40+
/*
41+
* Define a global instance of the video ID that will be posted as a
42+
* bulletin into the user's channel feed. In practice, you will probably
43+
* retrieve this value from a search or your app.
44+
*/
45+
private static String VIDEO_ID = "L-oNKK1CrnU";
46+
47+
48+
/**
49+
* Authorize the user, call the youtube.channels.list method to retrieve
50+
* information about the user's YouTube channel, and post a bulletin with
51+
* a video ID to that channel.
52+
*
53+
* @param args command line args (not used).
54+
*/
55+
public static void main(String[] args) {
56+
57+
// This OAuth 2.0 access scope allows for full read/write access to the
58+
// authenticated user's account.
59+
List<String> scopes = Lists.newArrayList("https://www.googleapis.com/auth/youtube");
60+
61+
try {
62+
// Authorize the request.
63+
Credential credential = Auth.authorize(scopes, "channelbulletin");
64+
65+
// This object is used to make YouTube Data API requests.
66+
youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential).setApplicationName(
67+
"youtube-cmdline-channelbulletin-sample").build();
68+
69+
// Construct a request to retrieve the current user's channel ID.
70+
// See https://developers.google.com/youtube/v3/docs/channels/list
71+
YouTube.Channels.List channelRequest = youtube.channels().list("contentDetails");
72+
channelRequest.setMine(true);
73+
74+
// In the API response, only include channel information needed
75+
// for this use case.
76+
channelRequest.setFields("items/contentDetails");
77+
ChannelListResponse channelResult = channelRequest.execute();
78+
79+
List<Channel> channelsList = channelResult.getItems();
80+
81+
if (channelsList != null) {
82+
// The user's default channel is the first item in the list.
83+
String channelId = channelsList.get(0).getId();
84+
85+
// Create the snippet for the activity resource that
86+
// represents the channel bulletin. Set its channel ID
87+
// and description.
88+
ActivitySnippet snippet = new ActivitySnippet();
89+
snippet.setChannelId(channelId);
90+
Calendar cal = Calendar.getInstance();
91+
snippet.setDescription("Bulletin test video via YouTube API on " + cal.getTime());
92+
93+
// Create a resourceId that identifies the video ID. You could
94+
// set the kind to "youtube#playlist" and use a playlist ID
95+
// instead of a video ID.
96+
ResourceId resource = new ResourceId();
97+
resource.setKind("youtube#video");
98+
resource.setVideoId(VIDEO_ID);
99+
100+
ActivityContentDetailsBulletin bulletin = new ActivityContentDetailsBulletin();
101+
bulletin.setResourceId(resource);
102+
103+
// Construct the ActivityContentDetails object for the request.
104+
ActivityContentDetails contentDetails = new ActivityContentDetails();
105+
contentDetails.setBulletin(bulletin);
106+
107+
// Construct the resource, including the snippet and content
108+
// details, to send in the activities.insert
109+
Activity activity = new Activity();
110+
activity.setSnippet(snippet);
111+
activity.setContentDetails(contentDetails);
112+
113+
// The API request identifies the resource parts that are being
114+
// written (contentDetails and snippet). The API response will
115+
// also include those parts.
116+
YouTube.Activities.Insert insertActivities =
117+
youtube.activities().insert("contentDetails,snippet", activity);
118+
// Return the newly created activity resource.
119+
Activity newActivityInserted = insertActivities.execute();
120+
121+
if (newActivityInserted != null) {
122+
System.out.println(
123+
"New Activity inserted of type " + newActivityInserted.getSnippet().getType());
124+
System.out.println(" - Video id "
125+
+ newActivityInserted.getContentDetails().getBulletin().getResourceId().getVideoId());
126+
System.out.println(
127+
" - Description: " + newActivityInserted.getSnippet().getDescription());
128+
System.out.println(" - Posted on " + newActivityInserted.getSnippet().getPublishedAt());
129+
} else {
130+
System.out.println("Activity failed.");
131+
}
132+
133+
} else {
134+
System.out.println("No channels are assigned to this user.");
135+
}
136+
} catch (GoogleJsonResponseException e) {
137+
e.printStackTrace();
138+
System.err.println("There was a service error: " + e.getDetails().getCode() + " : "
139+
+ e.getDetails().getMessage());
140+
141+
} catch (Throwable t) {
142+
t.printStackTrace();
143+
}
144+
}
145+
}

0 commit comments

Comments
 (0)