Skip to content

Commit 8cc9e48

Browse files
"Added sample: java/src/main/java/com/google/api/services/samples/youtube/cmdline/data/PlaylistUpdates.java"
1 parent 095ac31 commit 8cc9e48

File tree

1 file changed

+165
-0
lines changed

1 file changed

+165
-0
lines changed
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
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
10+
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11+
* express or implied. See the License for the specific language governing permissions and
12+
* limitations under 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.io.IOException;
25+
import java.util.Calendar;
26+
import java.util.List;
27+
28+
/**
29+
* Creates a new, private playlist in the authorized user's channel and add
30+
* a video to that new playlist.
31+
*
32+
* @author Jeremy Walker
33+
*/
34+
public class PlaylistUpdates {
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+
* Define a global variable that identifies the video that will be added
44+
* to the new playlist.
45+
*/
46+
private static final String VIDEO_ID = "SZj6rAYkYOg";
47+
48+
/**
49+
* Authorize the user, create a playlist, and add an item to the playlist.
50+
*
51+
* @param args command line args (not used).
52+
*/
53+
public static void main(String[] args) {
54+
55+
// This OAuth 2.0 access scope allows for full read/write access to the
56+
// authenticated user's account.
57+
List<String> scopes = Lists.newArrayList("https://www.googleapis.com/auth/youtube");
58+
59+
try {
60+
// Authorize the request.
61+
Credential credential = Auth.authorize(scopes, "playlistupdates");
62+
63+
// This object is used to make YouTube Data API requests.
64+
youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential)
65+
.setApplicationName("youtube-cmdline-playlistupdates-sample")
66+
.build();
67+
68+
// Create a new, private playlist in the authorized user's channel.
69+
String playlistId = insertPlaylist();
70+
71+
// If a valid playlist was created, add a video to that playlist.
72+
insertPlaylistItem(playlistId, VIDEO_ID);
73+
74+
} catch (GoogleJsonResponseException e) {
75+
System.err.println("There was a service error: " + e.getDetails().getCode() + " : " + e.getDetails().getMessage());
76+
e.printStackTrace();
77+
} catch (IOException e) {
78+
System.err.println("IOException: " + e.getMessage());
79+
e.printStackTrace();
80+
} catch (Throwable t) {
81+
System.err.println("Throwable: " + t.getMessage());
82+
t.printStackTrace();
83+
}
84+
}
85+
86+
/**
87+
* Create a playlist and add it to the authorized account.
88+
*/
89+
private static String insertPlaylist() throws IOException {
90+
91+
// This code constructs the playlist resource that is being inserted.
92+
// It defines the playlist's title, description, and privacy status.
93+
PlaylistSnippet playlistSnippet = new PlaylistSnippet();
94+
playlistSnippet.setTitle("Test Playlist " + Calendar.getInstance().getTime());
95+
playlistSnippet.setDescription("A private playlist created with the YouTube API v3");
96+
PlaylistStatus playlistStatus = new PlaylistStatus();
97+
playlistStatus.setPrivacyStatus("private");
98+
99+
Playlist youTubePlaylist = new Playlist();
100+
youTubePlaylist.setSnippet(playlistSnippet);
101+
youTubePlaylist.setStatus(playlistStatus);
102+
103+
// Call the API to insert the new playlist. In the API call, the first
104+
// argument identifies the resource parts that the API response should
105+
// contain, and the second argument is the playlist being inserted.
106+
YouTube.Playlists.Insert playlistInsertCommand =
107+
youtube.playlists().insert("snippet,status", youTubePlaylist);
108+
Playlist playlistInserted = playlistInsertCommand.execute();
109+
110+
// Print data from the API response and return the new playlist's
111+
// unique playlist ID.
112+
System.out.println("New Playlist name: " + playlistInserted.getSnippet().getTitle());
113+
System.out.println(" - Privacy: " + playlistInserted.getStatus().getPrivacyStatus());
114+
System.out.println(" - Description: " + playlistInserted.getSnippet().getDescription());
115+
System.out.println(" - Posted: " + playlistInserted.getSnippet().getPublishedAt());
116+
System.out.println(" - Channel: " + playlistInserted.getSnippet().getChannelId() + "\n");
117+
return playlistInserted.getId();
118+
119+
}
120+
121+
/**
122+
* Create a playlist item with the specified video ID and add it to the
123+
* specified playlist.
124+
*
125+
* @param playlistId assign to newly created playlistitem
126+
* @param videoId YouTube video id to add to playlistitem
127+
*/
128+
private static String insertPlaylistItem(String playlistId, String videoId) throws IOException {
129+
130+
// Define a resourceId that identifies the video being added to the
131+
// playlist.
132+
ResourceId resourceId = new ResourceId();
133+
resourceId.setKind("youtube#video");
134+
resourceId.setVideoId(videoId);
135+
136+
// Set fields included in the playlistItem resource's "snippet" part.
137+
PlaylistItemSnippet playlistItemSnippet = new PlaylistItemSnippet();
138+
playlistItemSnippet.setTitle("First video in the test playlist");
139+
playlistItemSnippet.setPlaylistId(playlistId);
140+
playlistItemSnippet.setResourceId(resourceId);
141+
142+
// Create the playlistItem resource and set its snippet to the
143+
// object created above.
144+
PlaylistItem playlistItem = new PlaylistItem();
145+
playlistItem.setSnippet(playlistItemSnippet);
146+
147+
// Call the API to add the playlist item to the specified playlist.
148+
// In the API call, the first argument identifies the resource parts
149+
// that the API response should contain, and the second argument is
150+
// the playlist item being inserted.
151+
YouTube.PlaylistItems.Insert playlistItemsInsertCommand =
152+
youtube.playlistItems().insert("snippet,contentDetails", playlistItem);
153+
PlaylistItem returnedPlaylistItem = playlistItemsInsertCommand.execute();
154+
155+
// Print data from the API response and return the new playlist
156+
// item's unique playlistItem ID.
157+
158+
System.out.println("New PlaylistItem name: " + returnedPlaylistItem.getSnippet().getTitle());
159+
System.out.println(" - Video id: " + returnedPlaylistItem.getSnippet().getResourceId().getVideoId());
160+
System.out.println(" - Posted: " + returnedPlaylistItem.getSnippet().getPublishedAt());
161+
System.out.println(" - Channel: " + returnedPlaylistItem.getSnippet().getChannelId());
162+
return returnedPlaylistItem.getId();
163+
164+
}
165+
}

0 commit comments

Comments
 (0)