use of com.google.api.client.googleapis.json.GoogleJsonResponseException in project api-samples by youtube.
the class CommentHandling method main.
/**
* List, reply to comment threads; list, update, moderate, mark and delete
* replies.
*
* @param args command line args (not used).
*/
public static void main(String[] args) {
// This OAuth 2.0 access scope allows for full read/write access to the
// authenticated user's account and requires requests to use an SSL connection.
List<String> scopes = Lists.newArrayList("https://www.googleapis.com/auth/youtube.force-ssl");
try {
// Authorize the request.
Credential credential = Auth.authorize(scopes, "commentthreads");
// This object is used to make YouTube Data API requests.
youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential).setApplicationName("youtube-cmdline-commentthreads-sample").build();
// Prompt the user for the ID of a video to comment on.
// Retrieve the video ID that the user is commenting to.
String videoId = getVideoId();
System.out.println("You chose " + videoId + " to subscribe.");
// Prompt the user for the comment text.
// Retrieve the text that the user is commenting.
String text = getText();
System.out.println("You chose " + text + " to subscribe.");
// All the available methods are used in sequence just for the sake
// of an example.
// Call the YouTube Data API's commentThreads.list method to
// retrieve video comment threads.
CommentThreadListResponse videoCommentsListResponse = youtube.commentThreads().list("snippet").setVideoId(videoId).setTextFormat("plainText").execute();
List<CommentThread> videoComments = videoCommentsListResponse.getItems();
if (videoComments.isEmpty()) {
System.out.println("Can't get video comments.");
} else {
// Print information from the API response.
System.out.println("\n================== Returned Video Comments ==================\n");
for (CommentThread videoComment : videoComments) {
CommentSnippet snippet = videoComment.getSnippet().getTopLevelComment().getSnippet();
System.out.println(" - Author: " + snippet.getAuthorDisplayName());
System.out.println(" - Comment: " + snippet.getTextDisplay());
System.out.println("\n-------------------------------------------------------------\n");
}
CommentThread firstComment = videoComments.get(0);
// Will use this thread as parent to new reply.
String parentId = firstComment.getId();
// Create a comment snippet with text.
CommentSnippet commentSnippet = new CommentSnippet();
commentSnippet.setTextOriginal(text);
commentSnippet.setParentId(parentId);
// Create a comment with snippet.
Comment comment = new Comment();
comment.setSnippet(commentSnippet);
// Call the YouTube Data API's comments.insert method to reply
// to a comment.
// (If the intention is to create a new top-level comment,
// commentThreads.insert
// method should be used instead.)
Comment commentInsertResponse = youtube.comments().insert("snippet", comment).execute();
// Print information from the API response.
System.out.println("\n================== Created Comment Reply ==================\n");
CommentSnippet snippet = commentInsertResponse.getSnippet();
System.out.println(" - Author: " + snippet.getAuthorDisplayName());
System.out.println(" - Comment: " + snippet.getTextDisplay());
System.out.println("\n-------------------------------------------------------------\n");
// Call the YouTube Data API's comments.list method to retrieve
// existing comment
// replies.
CommentListResponse commentsListResponse = youtube.comments().list("snippet").setParentId(parentId).setTextFormat("plainText").execute();
List<Comment> comments = commentsListResponse.getItems();
if (comments.isEmpty()) {
System.out.println("Can't get comment replies.");
} else {
// Print information from the API response.
System.out.println("\n================== Returned Comment Replies ==================\n");
for (Comment commentReply : comments) {
snippet = commentReply.getSnippet();
System.out.println(" - Author: " + snippet.getAuthorDisplayName());
System.out.println(" - Comment: " + snippet.getTextDisplay());
System.out.println("\n-------------------------------------------------------------\n");
}
Comment firstCommentReply = comments.get(0);
firstCommentReply.getSnippet().setTextOriginal("updated");
Comment commentUpdateResponse = youtube.comments().update("snippet", firstCommentReply).execute();
// Print information from the API response.
System.out.println("\n================== Updated Video Comment ==================\n");
snippet = commentUpdateResponse.getSnippet();
System.out.println(" - Author: " + snippet.getAuthorDisplayName());
System.out.println(" - Comment: " + snippet.getTextDisplay());
System.out.println("\n-------------------------------------------------------------\n");
// Call the YouTube Data API's comments.setModerationStatus
// method to set moderation
// status of an existing comment.
youtube.comments().setModerationStatus(firstCommentReply.getId(), "published");
System.out.println(" - Changed comment status to published: " + firstCommentReply.getId());
// Call the YouTube Data API's comments.markAsSpam method to
// mark an existing comment as spam.
youtube.comments().markAsSpam(firstCommentReply.getId());
System.out.println(" - Marked comment as spam: " + firstCommentReply.getId());
// Call the YouTube Data API's comments.delete method to
// delete an existing comment.
youtube.comments().delete(firstCommentReply.getId());
System.out.println(" - Deleted comment as spam: " + firstCommentReply.getId());
}
}
} 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();
}
}
use of com.google.api.client.googleapis.json.GoogleJsonResponseException in project api-samples by youtube.
the class MyUploads method main.
/**
* Authorize the user, call the youtube.channels.list method to retrieve
* the playlist ID for the list of videos uploaded to the user's channel,
* and then call the youtube.playlistItems.list method to retrieve the
* list of videos in that playlist.
*
* @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, "myuploads");
// This object is used to make YouTube Data API requests.
youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential).setApplicationName("youtube-cmdline-myuploads-sample").build();
// Call the API's channels.list method to retrieve the
// resource that represents the authenticated user's channel.
// In the API response, only include channel information needed for
// this use case. The channel's contentDetails part contains
// playlist IDs relevant to the channel, including the ID for the
// list that contains videos uploaded to the channel.
YouTube.Channels.List channelRequest = youtube.channels().list("contentDetails");
channelRequest.setMine(true);
channelRequest.setFields("items/contentDetails,nextPageToken,pageInfo");
ChannelListResponse channelResult = channelRequest.execute();
List<Channel> channelsList = channelResult.getItems();
if (channelsList != null) {
// The user's default channel is the first item in the list.
// Extract the playlist ID for the channel's videos from the
// API response.
String uploadPlaylistId = channelsList.get(0).getContentDetails().getRelatedPlaylists().getUploads();
// Define a list to store items in the list of uploaded videos.
List<PlaylistItem> playlistItemList = new ArrayList<PlaylistItem>();
// Retrieve the playlist of the channel's uploaded videos.
YouTube.PlaylistItems.List playlistItemRequest = youtube.playlistItems().list("id,contentDetails,snippet");
playlistItemRequest.setPlaylistId(uploadPlaylistId);
// Only retrieve data used in this application, thereby making
// the application more efficient. See:
// https://developers.google.com/youtube/v3/getting-started#partial
playlistItemRequest.setFields("items(contentDetails/videoId,snippet/title,snippet/publishedAt),nextPageToken,pageInfo");
String nextToken = "";
// there are still more items to retrieve.
do {
playlistItemRequest.setPageToken(nextToken);
PlaylistItemListResponse playlistItemResult = playlistItemRequest.execute();
playlistItemList.addAll(playlistItemResult.getItems());
nextToken = playlistItemResult.getNextPageToken();
} while (nextToken != null);
// Prints information about the results.
prettyPrint(playlistItemList.size(), playlistItemList.iterator());
}
} catch (GoogleJsonResponseException e) {
e.printStackTrace();
System.err.println("There was a service error: " + e.getDetails().getCode() + " : " + e.getDetails().getMessage());
} catch (Throwable t) {
t.printStackTrace();
}
}
use of com.google.api.client.googleapis.json.GoogleJsonResponseException in project api-samples by youtube.
the class PlaylistUpdates method main.
/**
* Authorize the user, create a playlist, and add an item to the playlist.
*
* @param args command line args (not used).
*/
public static void main(String[] args) {
// This OAuth 2.0 access scope allows for full read/write access to the
// authenticated user's account.
List<String> scopes = Lists.newArrayList("https://www.googleapis.com/auth/youtube");
try {
// Authorize the request.
Credential credential = Auth.authorize(scopes, "playlistupdates");
// This object is used to make YouTube Data API requests.
youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential).setApplicationName("youtube-cmdline-playlistupdates-sample").build();
// Create a new, private playlist in the authorized user's channel.
String playlistId = insertPlaylist();
// If a valid playlist was created, add a video to that playlist.
insertPlaylistItem(playlistId, VIDEO_ID);
} catch (GoogleJsonResponseException e) {
System.err.println("There was a service error: " + 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();
}
}
use of com.google.api.client.googleapis.json.GoogleJsonResponseException in project api-samples by youtube.
the class ListLiveChatMessages method main.
/**
* Lists live chat messages and SuperChat details from a live broadcast.
*
* @param args videoId (optional). If the videoId is given, live chat messages will be retrieved
* from the chat associated with this video. If the videoId is not specified, the signed in
* user's current live broadcast will be used instead.
*/
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(YouTubeScopes.YOUTUBE_READONLY);
try {
// Authorize the request.
Credential credential = Auth.authorize(scopes, "listlivechatmessages");
// This object is used to make YouTube Data API requests.
youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential).setApplicationName("youtube-cmdline-listchatmessages-sample").build();
// Get the liveChatId
String liveChatId = args.length == 1 ? GetLiveChatId.getLiveChatId(youtube, args[0]) : GetLiveChatId.getLiveChatId(youtube);
if (liveChatId != null) {
System.out.println("Live chat id: " + liveChatId);
} else {
System.err.println("Unable to find a live chat id");
System.exit(1);
}
// Get live chat messages
listChatMessages(liveChatId, null, 0);
} 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();
}
}
use of com.google.api.client.googleapis.json.GoogleJsonResponseException in project api-samples by youtube.
the class ListStreams method main.
/**
* List streams for the user's channel.
*/
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, "liststreams");
// This object is used to make YouTube Data API requests.
youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential).setApplicationName("youtube-cmdline-liststreams-sample").build();
// Create a request to list liveStream resources.
YouTube.LiveStreams.List livestreamRequest = youtube.liveStreams().list("id,snippet");
// Modify results to only return the user's streams.
livestreamRequest.setMine(true);
// Execute the API request and return the list of streams.
LiveStreamListResponse returnedListResponse = livestreamRequest.execute();
List<LiveStream> returnedList = returnedListResponse.getItems();
// Print information from the API response.
System.out.println("\n================== Returned Streams ==================\n");
for (LiveStream stream : returnedList) {
System.out.println(" - Id: " + stream.getId());
System.out.println(" - Title: " + stream.getSnippet().getTitle());
System.out.println(" - Description: " + stream.getSnippet().getDescription());
System.out.println(" - Published At: " + stream.getSnippet().getPublishedAt());
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();
}
}
Aggregations