Skip to content

Commit 7d1035f

Browse files
"Added sample: java/src/main/java/com/google/api/services/samples/youtube/cmdline/live/ListBroadcasts.java"
1 parent 4c59999 commit 7d1035f

1 file changed

Lines changed: 100 additions & 0 deletions

File tree

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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.live;
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.LiveBroadcast;
22+
import com.google.api.services.youtube.model.LiveBroadcastListResponse;
23+
import com.google.common.collect.Lists;
24+
25+
import java.io.IOException;
26+
import java.util.List;
27+
28+
/**
29+
* Retrieve a list of a channel's broadcasts, using OAuth 2.0 to authorize
30+
* API requests.
31+
*
32+
* @author Ibrahim Ulukaya
33+
*/
34+
public class ListBroadcasts {
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+
* List broadcasts for the user's channel.
44+
*
45+
* @param args command line args (not used).
46+
*/
47+
public static void main(String[] args) {
48+
49+
// This OAuth 2.0 access scope allows for read-only access to the
50+
// authenticated user's account, but not other types of account access.
51+
List<String> scopes = Lists.newArrayList("https://www.googleapis.com/auth/youtube.readonly");
52+
53+
try {
54+
// Authorize the request.
55+
Credential credential = Auth.authorize(scopes, "listbroadcasts");
56+
57+
// This object is used to make YouTube Data API requests.
58+
youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential)
59+
.setApplicationName("youtube-cmdline-listbroadcasts-sample").build();
60+
61+
// Create a request to list broadcasts.
62+
YouTube.LiveBroadcasts.List liveBroadcastRequest =
63+
youtube.liveBroadcasts().list("id,snippet");
64+
65+
// Indicate that the API response should not filter broadcasts
66+
// based on their status.
67+
liveBroadcastRequest.setBroadcastStatus("all");
68+
69+
// Execute the API request and return the list of broadcasts.
70+
LiveBroadcastListResponse returnedListResponse = liveBroadcastRequest.execute();
71+
List<LiveBroadcast> returnedList = returnedListResponse.getItems();
72+
73+
// Print information from the API response.
74+
System.out.println("\n================== Returned Broadcasts ==================\n");
75+
for (LiveBroadcast broadcast : returnedList) {
76+
System.out.println(" - Id: " + broadcast.getId());
77+
System.out.println(" - Title: " + broadcast.getSnippet().getTitle());
78+
System.out.println(" - Description: " + broadcast.getSnippet().getDescription());
79+
System.out.println(" - Published At: " + broadcast.getSnippet().getPublishedAt());
80+
System.out.println(
81+
" - Scheduled Start Time: " + broadcast.getSnippet().getScheduledStartTime());
82+
System.out.println(
83+
" - Scheduled End Time: " + broadcast.getSnippet().getScheduledEndTime());
84+
System.out.println("\n-------------------------------------------------------------\n");
85+
}
86+
87+
} catch (GoogleJsonResponseException e) {
88+
System.err.println("GoogleJsonResponseException code: " + e.getDetails().getCode() + " : "
89+
+ e.getDetails().getMessage());
90+
e.printStackTrace();
91+
92+
} catch (IOException e) {
93+
System.err.println("IOException: " + e.getMessage());
94+
e.printStackTrace();
95+
} catch (Throwable t) {
96+
System.err.println("Throwable: " + t.getMessage());
97+
t.printStackTrace();
98+
}
99+
}
100+
}

0 commit comments

Comments
 (0)