Skip to content

Commit 61ad3ae

Browse files
"Added sample: java/src/main/java/com/google/api/services/samples/youtube/cmdline/data/MyUploads.java"
1 parent ab972ca commit 61ad3ae

1 file changed

Lines changed: 148 additions & 0 deletions

File tree

  • java/src/main/java/com/google/api/services/samples/youtube/cmdline/data
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
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.Channel;
22+
import com.google.api.services.youtube.model.ChannelListResponse;
23+
import com.google.api.services.youtube.model.PlaylistItem;
24+
import com.google.api.services.youtube.model.PlaylistItemListResponse;
25+
import com.google.common.collect.Lists;
26+
27+
import java.util.ArrayList;
28+
import java.util.Iterator;
29+
import java.util.List;
30+
31+
/**
32+
* Print a list of videos uploaded to the authenticated user's YouTube channel.
33+
*
34+
* @author Jeremy Walker
35+
*/
36+
public class MyUploads {
37+
38+
/**
39+
* Define a global instance of a Youtube object, which will be used
40+
* to make YouTube Data API requests.
41+
*/
42+
private static YouTube youtube;
43+
44+
/**
45+
* Authorize the user, call the youtube.channels.list method to retrieve
46+
* the playlist ID for the list of videos uploaded to the user's channel,
47+
* and then call the youtube.playlistItems.list method to retrieve the
48+
* list of videos in that playlist.
49+
*
50+
* @param args command line args (not used).
51+
*/
52+
public static void main(String[] args) {
53+
54+
// This OAuth 2.0 access scope allows for read-only access to the
55+
// authenticated user's account, but not other types of account access.
56+
List<String> scopes = Lists.newArrayList("https://www.googleapis.com/auth/youtube.readonly");
57+
58+
try {
59+
// Authorize the request.
60+
Credential credential = Auth.authorize(scopes, "myuploads");
61+
62+
// This object is used to make YouTube Data API requests.
63+
youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential).setApplicationName(
64+
"youtube-cmdline-myuploads-sample").build();
65+
66+
// Call the API's channels.list method to retrieve the
67+
// resource that represents the authenticated user's channel.
68+
// In the API response, only include channel information needed for
69+
// this use case. The channel's contentDetails part contains
70+
// playlist IDs relevant to the channel, including the ID for the
71+
// list that contains videos uploaded to the channel.
72+
YouTube.Channels.List channelRequest = youtube.channels().list("contentDetails");
73+
channelRequest.setMine(true);
74+
channelRequest.setFields("items/contentDetails,nextPageToken,pageInfo");
75+
ChannelListResponse channelResult = channelRequest.execute();
76+
77+
List<Channel> channelsList = channelResult.getItems();
78+
79+
if (channelsList != null) {
80+
// The user's default channel is the first item in the list.
81+
// Extract the playlist ID for the channel's videos from the
82+
// API response.
83+
String uploadPlaylistId =
84+
channelsList.get(0).getContentDetails().getRelatedPlaylists().getUploads();
85+
86+
// Define a list to store items in the list of uploaded videos.
87+
List<PlaylistItem> playlistItemList = new ArrayList<PlaylistItem>();
88+
89+
// Retrieve the playlist of the channel's uploaded videos.
90+
YouTube.PlaylistItems.List playlistItemRequest =
91+
youtube.playlistItems().list("id,contentDetails,snippet");
92+
playlistItemRequest.setPlaylistId(uploadPlaylistId);
93+
94+
// Only retrieve data used in this application, thereby making
95+
// the application more efficient. See:
96+
// https://developers.google.com/youtube/v3/getting-started#partial
97+
playlistItemRequest.setFields(
98+
"items(contentDetails/videoId,snippet/title,snippet/publishedAt),nextPageToken,pageInfo");
99+
100+
String nextToken = "";
101+
102+
// Call the API one or more times to retrieve all items in the
103+
// list. As long as the API response returns a nextPageToken,
104+
// there are still more items to retrieve.
105+
do {
106+
playlistItemRequest.setPageToken(nextToken);
107+
PlaylistItemListResponse playlistItemResult = playlistItemRequest.execute();
108+
109+
playlistItemList.addAll(playlistItemResult.getItems());
110+
111+
nextToken = playlistItemResult.getNextPageToken();
112+
} while (nextToken != null);
113+
114+
// Prints information about the results.
115+
prettyPrint(playlistItemList.size(), playlistItemList.iterator());
116+
}
117+
118+
} catch (GoogleJsonResponseException e) {
119+
e.printStackTrace();
120+
System.err.println("There was a service error: " + e.getDetails().getCode() + " : "
121+
+ e.getDetails().getMessage());
122+
123+
} catch (Throwable t) {
124+
t.printStackTrace();
125+
}
126+
}
127+
128+
/*
129+
* Print information about all of the items in the playlist.
130+
*
131+
* @param size size of list
132+
*
133+
* @param iterator of Playlist Items from uploaded Playlist
134+
*/
135+
private static void prettyPrint(int size, Iterator<PlaylistItem> playlistEntries) {
136+
System.out.println("=============================================================");
137+
System.out.println("\t\tTotal Videos Uploaded: " + size);
138+
System.out.println("=============================================================\n");
139+
140+
while (playlistEntries.hasNext()) {
141+
PlaylistItem playlistItem = playlistEntries.next();
142+
System.out.println(" video name = " + playlistItem.getSnippet().getTitle());
143+
System.out.println(" video id = " + playlistItem.getContentDetails().getVideoId());
144+
System.out.println(" upload date = " + playlistItem.getSnippet().getPublishedAt());
145+
System.out.println("\n-------------------------------------------------------------\n");
146+
}
147+
}
148+
}

0 commit comments

Comments
 (0)