Search in sources :

Example 81 with GoogleJsonResponseException

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();
    }
}
Also used : CommentSnippet(com.google.api.services.youtube.model.CommentSnippet) Comment(com.google.api.services.youtube.model.Comment) Credential(com.google.api.client.auth.oauth2.Credential) IOException(java.io.IOException) YouTube(com.google.api.services.youtube.YouTube) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) CommentThreadListResponse(com.google.api.services.youtube.model.CommentThreadListResponse) CommentThread(com.google.api.services.youtube.model.CommentThread) CommentListResponse(com.google.api.services.youtube.model.CommentListResponse)

Example 82 with GoogleJsonResponseException

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();
    }
}
Also used : Credential(com.google.api.client.auth.oauth2.Credential) Channel(com.google.api.services.youtube.model.Channel) ArrayList(java.util.ArrayList) YouTube(com.google.api.services.youtube.YouTube) PlaylistItemListResponse(com.google.api.services.youtube.model.PlaylistItemListResponse) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) ChannelListResponse(com.google.api.services.youtube.model.ChannelListResponse) PlaylistItem(com.google.api.services.youtube.model.PlaylistItem)

Example 83 with GoogleJsonResponseException

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();
    }
}
Also used : GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) Credential(com.google.api.client.auth.oauth2.Credential) IOException(java.io.IOException) YouTube(com.google.api.services.youtube.YouTube)

Example 84 with GoogleJsonResponseException

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();
    }
}
Also used : GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) Credential(com.google.api.client.auth.oauth2.Credential) IOException(java.io.IOException) YouTube(com.google.api.services.youtube.YouTube)

Example 85 with GoogleJsonResponseException

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();
    }
}
Also used : Credential(com.google.api.client.auth.oauth2.Credential) IOException(java.io.IOException) YouTube(com.google.api.services.youtube.YouTube) LiveStreamListResponse(com.google.api.services.youtube.model.LiveStreamListResponse) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) LiveStream(com.google.api.services.youtube.model.LiveStream)

Aggregations

GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)94 IOException (java.io.IOException)48 YouTube (com.google.api.services.youtube.YouTube)26 Credential (com.google.api.client.auth.oauth2.Credential)25 ArrayList (java.util.ArrayList)13 Operation (com.google.api.services.compute.model.Operation)12 Test (org.junit.Test)12 Compute (com.google.api.services.compute.Compute)11 Storage (com.google.api.services.storage.Storage)10 GcpResourceException (com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException)8 GcsOptions (org.apache.beam.sdk.extensions.gcp.options.GcsOptions)7 GoogleJsonError (com.google.api.client.googleapis.json.GoogleJsonError)5 InputStreamContent (com.google.api.client.http.InputStreamContent)5 BackOff (com.google.api.client.util.BackOff)5 HashMap (java.util.HashMap)5 Objects (com.google.api.services.storage.model.Objects)4 GoogleCloudStorage (com.google.cloud.hadoop.gcsio.GoogleCloudStorage)4 HalException (com.netflix.spinnaker.halyard.core.error.v1.HalException)4 MediaHttpUploader (com.google.api.client.googleapis.media.MediaHttpUploader)3 MediaHttpUploaderProgressListener (com.google.api.client.googleapis.media.MediaHttpUploaderProgressListener)3