Repository navigation
Expand file tree
/
Copy pathListBroadcasts.java
More file actions
100 lines (85 loc) · 4.26 KB
/
Copy pathListBroadcasts.java
File metadata and controls
100 lines (85 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
* Copyright (c) 2013 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.api.services.samples.youtube.cmdline.live;
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
import com.google.api.services.samples.youtube.cmdline.Auth;
import com.google.api.services.youtube.YouTube;
import com.google.api.services.youtube.model.LiveBroadcast;
import com.google.api.services.youtube.model.LiveBroadcastListResponse;
import com.google.common.collect.Lists;
import java.io.IOException;
import java.util.List;
/**
* Retrieve a list of a channel's broadcasts, using OAuth 2.0 to authorize
* API requests.
*
* @author Ibrahim Ulukaya
*/
public class ListBroadcasts {
/**
* Define a global instance of a Youtube object, which will be used
* to make YouTube Data API requests.
*/
private static YouTube youtube;
/**
* List broadcasts for the user's channel.
*
* @param args command line args (not used).
*/
public static void main(String[] args) {
// This OAuth 2.0 access scope allows for read-only access to the
// authenticated user's account, but not other types of account access.
List<String> scopes = Lists.newArrayList("https://www.googleapis.com/auth/youtube.readonly");
try {
// Authorize the request.
Credential credential = Auth.authorize(scopes, "listbroadcasts");
// This object is used to make YouTube Data API requests.
youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential)
.setApplicationName("youtube-cmdline-listbroadcasts-sample").build();
// Create a request to list broadcasts.
YouTube.LiveBroadcasts.List liveBroadcastRequest =
youtube.liveBroadcasts().list("id,snippet");
// Indicate that the API response should not filter broadcasts
// based on their type or status.
liveBroadcastRequest.setBroadcastType("all").setBroadcastStatus("all");
// Execute the API request and return the list of broadcasts.
LiveBroadcastListResponse returnedListResponse = liveBroadcastRequest.execute();
List<LiveBroadcast> returnedList = returnedListResponse.getItems();
// Print information from the API response.
System.out.println("\n================== Returned Broadcasts ==================\n");
for (LiveBroadcast broadcast : returnedList) {
System.out.println(" - Id: " + broadcast.getId());
System.out.println(" - Title: " + broadcast.getSnippet().getTitle());
System.out.println(" - Description: " + broadcast.getSnippet().getDescription());
System.out.println(" - Published At: " + broadcast.getSnippet().getPublishedAt());
System.out.println(
" - Scheduled Start Time: " + broadcast.getSnippet().getScheduledStartTime());
System.out.println(
" - Scheduled End Time: " + broadcast.getSnippet().getScheduledEndTime());
System.out.println("\n-------------------------------------------------------------\n");
}
} catch (GoogleJsonResponseException e) {
System.err.println("GoogleJsonResponseException code: " + e.getDetails().getCode() + " : "
+ e.getDetails().getMessage());
e.printStackTrace();
} catch (IOException e) {
System.err.println("IOException: " + e.getMessage());
e.printStackTrace();
} catch (Throwable t) {
System.err.println("Throwable: " + t.getMessage());
t.printStackTrace();
}
}
}