Search in sources :

Example 26 with Credential

use of com.google.api.client.auth.oauth2.Credential 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 27 with Credential

use of com.google.api.client.auth.oauth2.Credential in project api-samples by youtube.

the class ChannelBulletin method main.

/**
     * Authorize the user, call the youtube.channels.list method to retrieve
     * information about the user's YouTube channel, and post a bulletin with
     * a video ID to that channel.
     *
     * @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, "channelbulletin");
        // This object is used to make YouTube Data API requests.
        youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential).setApplicationName("youtube-cmdline-channelbulletin-sample").build();
        // Construct a request to retrieve the current user's channel ID.
        // See https://developers.google.com/youtube/v3/docs/channels/list
        YouTube.Channels.List channelRequest = youtube.channels().list("contentDetails");
        channelRequest.setMine(true);
        // In the API response, only include channel information needed
        // for this use case.
        channelRequest.setFields("items/contentDetails");
        ChannelListResponse channelResult = channelRequest.execute();
        List<Channel> channelsList = channelResult.getItems();
        if (channelsList != null) {
            // The user's default channel is the first item in the list.
            String channelId = channelsList.get(0).getId();
            // Create the snippet for the activity resource that
            // represents the channel bulletin. Set its channel ID
            // and description.
            ActivitySnippet snippet = new ActivitySnippet();
            snippet.setChannelId(channelId);
            Calendar cal = Calendar.getInstance();
            snippet.setDescription("Bulletin test video via YouTube API on " + cal.getTime());
            // Create a resourceId that identifies the video ID. You could
            // set the kind to "youtube#playlist" and use a playlist ID
            // instead of a video ID.
            ResourceId resource = new ResourceId();
            resource.setKind("youtube#video");
            resource.setVideoId(VIDEO_ID);
            ActivityContentDetailsBulletin bulletin = new ActivityContentDetailsBulletin();
            bulletin.setResourceId(resource);
            // Construct the ActivityContentDetails object for the request.
            ActivityContentDetails contentDetails = new ActivityContentDetails();
            contentDetails.setBulletin(bulletin);
            // Construct the resource, including the snippet and content
            // details, to send in the activities.insert
            Activity activity = new Activity();
            activity.setSnippet(snippet);
            activity.setContentDetails(contentDetails);
            // The API request identifies the resource parts that are being
            // written (contentDetails and snippet). The API response will
            // also include those parts.
            YouTube.Activities.Insert insertActivities = youtube.activities().insert("contentDetails,snippet", activity);
            // Return the newly created activity resource.
            Activity newActivityInserted = insertActivities.execute();
            if (newActivityInserted != null) {
                System.out.println("New Activity inserted of type " + newActivityInserted.getSnippet().getType());
                System.out.println(" - Video id " + newActivityInserted.getContentDetails().getBulletin().getResourceId().getVideoId());
                System.out.println(" - Description: " + newActivityInserted.getSnippet().getDescription());
                System.out.println(" - Posted on " + newActivityInserted.getSnippet().getPublishedAt());
            } else {
                System.out.println("Activity failed.");
            }
        } else {
            System.out.println("No channels are assigned to this user.");
        }
    } 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) Calendar(java.util.Calendar) YouTube(com.google.api.services.youtube.YouTube) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException)

Example 28 with Credential

use of com.google.api.client.auth.oauth2.Credential in project api-samples by youtube.

the class ChannelLocalizations method main.

/**
     * Set and retrieve localized metadata for a channel.
     *
     * @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, "localizations");
        // This object is used to make YouTube Data API requests.
        youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential).setApplicationName("youtube-cmdline-localizations-sample").build();
        // Prompt the user to specify the action of the be achieved.
        String actionString = getActionFromUser();
        System.out.println("You chose " + actionString + ".");
        //Map the user input to the enum values.
        Action action = Action.valueOf(actionString.toUpperCase());
        switch(action) {
            case SET:
                setChannelLocalization(getId("channel"), getDefaultLanguage(), getLanguage(), getMetadata("description"));
                break;
            case GET:
                getChannelLocalization(getId("channel"), getLanguage());
                break;
            case LIST:
                listChannelLocalizations(getId("channel"));
                break;
        }
    } 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 29 with Credential

use of com.google.api.client.auth.oauth2.Credential 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 30 with Credential

use of com.google.api.client.auth.oauth2.Credential in project api-samples by youtube.

the class CommentThreads method main.

/**
     * Create, list and update top-level channel and video comments.
     *
     * @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 channel to comment on.
        // Retrieve the channel ID that the user is commenting to.
        String channelId = getChannelId();
        System.out.println("You chose " + channelId + " to subscribe.");
        // 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.");
        // Insert channel comment by omitting videoId.
        // Create a comment snippet with text.
        CommentSnippet commentSnippet = new CommentSnippet();
        commentSnippet.setTextOriginal(text);
        // Create a top-level comment with snippet.
        Comment topLevelComment = new Comment();
        topLevelComment.setSnippet(commentSnippet);
        // Create a comment thread snippet with channelId and top-level
        // comment.
        CommentThreadSnippet commentThreadSnippet = new CommentThreadSnippet();
        commentThreadSnippet.setChannelId(channelId);
        commentThreadSnippet.setTopLevelComment(topLevelComment);
        // Create a comment thread with snippet.
        CommentThread commentThread = new CommentThread();
        commentThread.setSnippet(commentThreadSnippet);
        // Call the YouTube Data API's commentThreads.insert method to
        // create a comment.
        CommentThread channelCommentInsertResponse = youtube.commentThreads().insert("snippet", commentThread).execute();
        // Print information from the API response.
        System.out.println("\n================== Created Channel Comment ==================\n");
        CommentSnippet snippet = channelCommentInsertResponse.getSnippet().getTopLevelComment().getSnippet();
        System.out.println("  - Author: " + snippet.getAuthorDisplayName());
        System.out.println("  - Comment: " + snippet.getTextDisplay());
        System.out.println("\n-------------------------------------------------------------\n");
        // Insert video comment
        commentThreadSnippet.setVideoId(videoId);
        // Call the YouTube Data API's commentThreads.insert method to
        // create a comment.
        CommentThread videoCommentInsertResponse = youtube.commentThreads().insert("snippet", commentThread).execute();
        // Print information from the API response.
        System.out.println("\n================== Created Video Comment ==================\n");
        snippet = videoCommentInsertResponse.getSnippet().getTopLevelComment().getSnippet();
        System.out.println("  - Author: " + snippet.getAuthorDisplayName());
        System.out.println("  - Comment: " + snippet.getTextDisplay());
        System.out.println("\n-------------------------------------------------------------\n");
        // 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) {
                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);
            firstComment.getSnippet().getTopLevelComment().getSnippet().setTextOriginal("updated");
            CommentThread videoCommentUpdateResponse = youtube.commentThreads().update("snippet", firstComment).execute();
            // Print information from the API response.
            System.out.println("\n================== Updated Video Comment ==================\n");
            snippet = videoCommentUpdateResponse.getSnippet().getTopLevelComment().getSnippet();
            System.out.println("  - Author: " + snippet.getAuthorDisplayName());
            System.out.println("  - Comment: " + snippet.getTextDisplay());
            System.out.println("\n-------------------------------------------------------------\n");
        }
        // Call the YouTube Data API's commentThreads.list method to
        // retrieve channel comment threads.
        CommentThreadListResponse channelCommentsListResponse = youtube.commentThreads().list("snippet").setChannelId(channelId).setTextFormat("plainText").execute();
        List<CommentThread> channelComments = channelCommentsListResponse.getItems();
        if (channelComments.isEmpty()) {
            System.out.println("Can't get channel comments.");
        } else {
            // Print information from the API response.
            System.out.println("\n================== Returned Channel Comments ==================\n");
            for (CommentThread channelComment : channelComments) {
                snippet = channelComment.getSnippet().getTopLevelComment().getSnippet();
                System.out.println("  - Author: " + snippet.getAuthorDisplayName());
                System.out.println("  - Comment: " + snippet.getTextDisplay());
                System.out.println("\n-------------------------------------------------------------\n");
            }
            CommentThread firstComment = channelComments.get(0);
            firstComment.getSnippet().getTopLevelComment().getSnippet().setTextOriginal("updated");
            CommentThread channelCommentUpdateResponse = youtube.commentThreads().update("snippet", firstComment).execute();
            // Print information from the API response.
            System.out.println("\n================== Updated Channel Comment ==================\n");
            snippet = channelCommentUpdateResponse.getSnippet().getTopLevelComment().getSnippet();
            System.out.println("  - Author: " + snippet.getAuthorDisplayName());
            System.out.println("  - Comment: " + snippet.getTextDisplay());
            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 : CommentThreadSnippet(com.google.api.services.youtube.model.CommentThreadSnippet) 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) CommentThread(com.google.api.services.youtube.model.CommentThread) CommentThreadListResponse(com.google.api.services.youtube.model.CommentThreadListResponse)

Aggregations

Credential (com.google.api.client.auth.oauth2.Credential)33 IOException (java.io.IOException)24 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)21 YouTube (com.google.api.services.youtube.YouTube)20 StoredCredential (com.google.api.client.auth.oauth2.StoredCredential)5 InputStreamContent (com.google.api.client.http.InputStreamContent)4 AuthorizationCodeInstalledApp (com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp)3 LocalServerReceiver (com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver)3 GoogleAuthorizationCodeFlow (com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow)3 GoogleClientSecrets (com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets)3 MediaHttpUploader (com.google.api.client.googleapis.media.MediaHttpUploader)3 MediaHttpUploaderProgressListener (com.google.api.client.googleapis.media.MediaHttpUploaderProgressListener)3 FileDataStoreFactory (com.google.api.client.util.store.FileDataStoreFactory)3 ArrayList (java.util.ArrayList)3 Calendar (java.util.Calendar)3 GoogleCredential (com.google.api.client.googleapis.auth.oauth2.GoogleCredential)2 HttpRequest (com.google.api.client.http.HttpRequest)2 DateTime (com.google.api.client.util.DateTime)2 Channel (com.google.api.services.youtube.model.Channel)2 ChannelListResponse (com.google.api.services.youtube.model.ChannelListResponse)2