Skip to content

Commit 48b4230

Browse files
author
Marc Chambers
authored
Merge pull request youtube#110 from jimrogerz/live_chat
Add samples for live chat
2 parents 0b62f83 + cf7f9b3 commit 48b4230

6 files changed

Lines changed: 578 additions & 5 deletions

File tree

java/README.md

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,15 @@ client ID and client secret. You can create an ID/secret pair at:
99

1010
To build this code sample from the command line, type:
1111

12-
mvn compile
12+
<code>mvn compile</code>
1313

14-
To run the code sample from the command line, enter the following:
14+
To run a code sample from the command line, enter the following:
1515

16-
mvn exec:java -Dexec.mainClass="FULL_CLASS_NAME"
16+
<code>mvn exec:java -Dexec.mainClass="FULL_CLASS_NAME"</code>
17+
18+
For samples that require arguments, also specify -Dexec.args, e.g.:
19+
20+
<code>mvn exec:java -Dexec.mainClass="FULL_CLASS_NAME" -Dexec.args="arg1 arg2"</code>
1721

1822
For more instructions about how to set up Maven and/or your IDE to run
1923
YouTube API samples, see this video:
@@ -214,7 +218,7 @@ job.
214218
Method: youtubeReporting.jobs.list, youtubeReporting.reports.list<br>
215219
Description: This sample demonstrates how to retrieve reports created by a specific job. It calls the
216220
<code>jobs.list</code> method to retrieve reporting jobs. It then calls the <code>reports.list</code> method with the
217-
<code>jobId</code> parameter set to a specific job id to retrieve reports created by that job. Finally, the sample
221+
<code>jobId</code> parameter set to a specific job ID to retrieve reports created by that job. Finally, the sample
218222
prints out the download URL for each report.
219223

220224
### [Create a broadcast and stream](/java/src/main/java/com/google/api/services/samples/youtube/cmdline/live/CreateBroadcast.java)
@@ -236,3 +240,28 @@ also specify a value for the <code>--broadcast-status</code> option to only retr
236240
Method: youtube.liveStreams.list<br>
237241
Description: This sample calls the API's <code>liveStreams.list</code> method to retrieve a list of video stream settings
238242
that a channel can use to broadcast live events on YouTube.
243+
244+
245+
### [Get a live chat id](/java/src/main/java/com/google/api/services/samples/youtube/cmdline/live/GetLiveChatId.java)
246+
247+
Methods: youtube.videos.list, youtube.liveBroadcasts.list<br>
248+
Description: This sample retrieves the live chat ID from either a <code>videoId</code> parameter
249+
or the live broadcast for the authorized user's channel. The <code>liveChatId</code> is required for other samples
250+
that interact with live chat.
251+
252+
### [Insert a live chat message](/java/src/main/java/com/google/api/services/samples/youtube/cmdline/live/InsertLiveChatMessage.java)
253+
254+
Method: youtube.liveChatMessages.insert<br>
255+
Description: This sample inserts a live chat message into the the specified video or the live broadcast for
256+
the authorized user's channel.
257+
258+
### [Delete a live chat message](/java/src/main/java/com/google/api/services/samples/youtube/cmdline/live/DeleteLiveChatMessage.java)
259+
260+
Method: youtube.liveChatMessages.delete<br>
261+
Description: This sample deletes the specified live chat message.
262+
263+
### [List live chat messages](/java/src/main/java/com/google/api/services/samples/youtube/cmdline/live/ListLiveChatMessages.java)
264+
265+
Method: youtube.liveChatMessages.list<br>
266+
Description: This sample lists live chat messages from the specified video or from the live broadcast for
267+
the authorized user's channel.

java/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<url>http://maven.apache.org</url>
1212

1313
<properties>
14-
<project.youtube.version>v3-rev179-1.22.0</project.youtube.version>
14+
<project.youtube.version>v3-rev182-1.22.0</project.youtube.version>
1515
<project.youtube.analytics.version>v1-rev63-1.22.0</project.youtube.analytics.version>
1616
<project.youtube.reporting.version>v1-rev10-1.22.0</project.youtube.reporting.version>
1717
<project.http.version>1.20.0</project.http.version>
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright (c) 2017 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.YouTubeScopes;
22+
import com.google.common.collect.Lists;
23+
import java.io.IOException;
24+
import java.util.List;
25+
26+
/**
27+
* Delets a message from a live broadcast, using OAuth 2.0 to authorize API requests.
28+
*
29+
* @author Jim Rogers
30+
*/
31+
public class DeleteLiveChatMessage {
32+
33+
/**
34+
* Define a global instance of a Youtube object, which will be used
35+
* to make YouTube Data API requests.
36+
*/
37+
private static YouTube youtube;
38+
39+
/**
40+
* Deletes a message from a live broadcast.
41+
*
42+
* @param args The message id to delete (required) followed by the videoId (optional). If the
43+
* videoId is given, live chat messages will be retrieved from the chat associated with this
44+
* video. If the videoId is not specified, the signed in user's current live broadcast will be
45+
* used instead.
46+
*/
47+
public static void main(String[] args) {
48+
// Get the message id to delete
49+
if (args.length == 0) {
50+
System.err.println("No message id specified");
51+
System.exit(1);
52+
}
53+
String messageId = args[0];
54+
55+
// This OAuth 2.0 access scope allows for write access to the authenticated user's account.
56+
List<String> scopes = Lists.newArrayList(YouTubeScopes.YOUTUBE_FORCE_SSL);
57+
58+
try {
59+
// Authorize the request.
60+
Credential credential = Auth.authorize(scopes, "deletelivechatmessage");
61+
62+
// This object is used to make YouTube Data API requests.
63+
youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential)
64+
.setApplicationName("youtube-cmdline-deletechatmessages-sample").build();
65+
66+
// Delete the message from live chat
67+
YouTube.LiveChatMessages.Delete liveChatDelete =
68+
youtube.liveChatMessages().delete(messageId);
69+
liveChatDelete.execute();
70+
System.out.println("Deleted message id " + messageId);
71+
} catch (GoogleJsonResponseException e) {
72+
System.err
73+
.println("GoogleJsonResponseException code: " + e.getDetails().getCode() + " : "
74+
+ e.getDetails().getMessage());
75+
e.printStackTrace();
76+
} catch (IOException e) {
77+
System.err.println("IOException: " + e.getMessage());
78+
e.printStackTrace();
79+
} catch (Throwable t) {
80+
System.err.println("Throwable: " + t.getMessage());
81+
t.printStackTrace();
82+
}
83+
}
84+
}
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/*
2+
* Copyright (c) 2017 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.YouTubeScopes;
22+
import com.google.api.services.youtube.model.LiveBroadcast;
23+
import com.google.api.services.youtube.model.LiveBroadcastListResponse;
24+
import com.google.api.services.youtube.model.Video;
25+
import com.google.api.services.youtube.model.VideoListResponse;
26+
import com.google.common.collect.Lists;
27+
import java.io.IOException;
28+
import java.util.List;
29+
30+
/**
31+
* Gets a live chat id from a video id or current signed in user.
32+
*
33+
* The videoId is often included in the video's url, e.g.:
34+
* https://www.youtube.com/watch?v=L5Xc93_ZL60
35+
* ^ videoId
36+
* The video URL may be found in the browser address bar, or by right-clicking a video and selecting
37+
* Copy video URL from the context menu.
38+
*
39+
* @author Jim Rogers
40+
*/
41+
public class GetLiveChatId {
42+
43+
/**
44+
* Define a global instance of a Youtube object, which will be used
45+
* to make YouTube Data API requests.
46+
*/
47+
private static YouTube youtube;
48+
49+
/**
50+
* Poll live chat messages and SuperChat details from a live broadcast.
51+
*
52+
* @param args videoId (optional). If the videoId is given, live chat messages will be retrieved
53+
* from the chat associated with this video. If the videoId is not specified, the signed in
54+
* user's current live broadcast will be used instead.
55+
*/
56+
public static void main(String[] args) {
57+
58+
// This OAuth 2.0 access scope allows for read-only access to the
59+
// authenticated user's account, but not other types of account access.
60+
List<String> scopes = Lists.newArrayList(YouTubeScopes.YOUTUBE_READONLY);
61+
62+
try {
63+
// Authorize the request.
64+
Credential credential = Auth.authorize(scopes, "getlivechatid");
65+
66+
// This object is used to make YouTube Data API requests.
67+
youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential)
68+
.setApplicationName("youtube-cmdline-getlivechatid-sample").build();
69+
70+
// Get the liveChatId
71+
String liveChatId = args.length == 1
72+
? getLiveChatId(youtube, args[0])
73+
: getLiveChatId(youtube);
74+
if (liveChatId != null) {
75+
System.out.println("Live chat id: " + liveChatId);
76+
} else {
77+
System.err.println("Unable to find a live chat id");
78+
System.exit(1);
79+
}
80+
} catch (GoogleJsonResponseException e) {
81+
System.err
82+
.println("GoogleJsonResponseException code: " + e.getDetails().getCode() + " : "
83+
+ e.getDetails().getMessage());
84+
e.printStackTrace();
85+
86+
} catch (IOException e) {
87+
System.err.println("IOException: " + e.getMessage());
88+
e.printStackTrace();
89+
} catch (Throwable t) {
90+
System.err.println("Throwable: " + t.getMessage());
91+
t.printStackTrace();
92+
}
93+
}
94+
95+
/**
96+
* Retrieves the liveChatId from the authenticated user's live broadcast.
97+
*
98+
* @param youtube The object is used to make YouTube Data API requests.
99+
* @return A liveChatId, or null if not found.
100+
*/
101+
static String getLiveChatId(YouTube youtube) throws IOException {
102+
// Get signed in user's liveChatId
103+
YouTube.LiveBroadcasts.List broadcastList = youtube
104+
.liveBroadcasts()
105+
.list("snippet")
106+
.setFields("items/snippet/liveChatId")
107+
.setBroadcastType("all")
108+
.setBroadcastStatus("active");
109+
LiveBroadcastListResponse broadcastListResponse = broadcastList.execute();
110+
for (LiveBroadcast b : broadcastListResponse.getItems()) {
111+
String liveChatId = b.getSnippet().getLiveChatId();
112+
if (liveChatId != null && !liveChatId.isEmpty()) {
113+
return liveChatId;
114+
}
115+
}
116+
117+
return null;
118+
}
119+
120+
/**
121+
* Retrieves the liveChatId from the broadcast associated with a videoId.
122+
*
123+
* @param youtube The object is used to make YouTube Data API requests.
124+
* @param videoId The videoId associated with the live broadcast.
125+
* @return A liveChatId, or null if not found.
126+
*/
127+
static String getLiveChatId(YouTube youtube, String videoId) throws IOException {
128+
// Get liveChatId from the video
129+
YouTube.Videos.List videoList = youtube.videos()
130+
.list("liveStreamingDetails")
131+
.setFields("items/liveStreamingDetails/activeLiveChatId")
132+
.setId(videoId);
133+
VideoListResponse response = videoList.execute();
134+
for (Video v : response.getItems()) {
135+
String liveChatId = v.getLiveStreamingDetails().getActiveLiveChatId();
136+
if (liveChatId != null && !liveChatId.isEmpty()) {
137+
return liveChatId;
138+
}
139+
}
140+
141+
return null;
142+
}
143+
}

0 commit comments

Comments
 (0)