📄 bloggerclient.java
字号:
* updating the values of the entry object before submitting the request. * * @param myService An authenticated GoogleService object. * @param entryToUpdate An Entry containing the post to update. * @param newTitle Text to use for the post's new title. * @return An Entry containing the newly-updated post. * @throws ServiceException If the service is unable to handle the request. * @throws IOException If the URL is malformed. */ public static Entry updatePostTitle(GoogleService myService, Entry entryToUpdate, String newTitle) throws ServiceException, IOException { entryToUpdate.setTitle(new PlainTextConstruct(newTitle)); URL editUrl = new URL(entryToUpdate.getEditLink().getHref()); return myService.update(editUrl, entryToUpdate); } /** * Adds a comment to the specified post. First the comment feed's URI is built * using the given post ID. Then an Entry is created for the comment and * submitted to the GoogleService. * * NOTE: This functionality is not officially supported yet. * * @param myService An authenticated GoogleService object. * @param postId The ID of the post to comment on. * @param commentText Text to store in the comment. * @return An entry containing the newly-created comment. * @throws ServiceException If the service is unable to handle the request. * @throws IOException If the URL is malformed. */ public static Entry createComment(GoogleService myService, String postId, String commentText) throws ServiceException, IOException { // Build the comment feed URI String commentsFeedUri = feedUri + "/" + postId + COMMENTS_FEED_URI_SUFFIX; URL feedUrl = new URL(commentsFeedUri); // Create a new entry for the comment and submit it to the GoogleService Entry myEntry = new Entry(); myEntry.setContent(new PlainTextConstruct(commentText)); return myService.insert(feedUrl, myEntry); } /** * Displays all the comments for the given post. First the comment feed's URI * is built using the given post ID. Then the method requests the comments * feed and displays the results. * * @param myService An authenticated GoogleService object. * @param postId The ID of the post to view comments on. * @throws ServiceException If the service is unable to handle the request. * @throws IOException If there is an error communicating with the server. */ public static void printAllComments(GoogleService myService, String postId) throws ServiceException, IOException { // Build comment feed URI and request comments on the specified post String commentsFeedUri = feedUri + "/" + postId + COMMENTS_FEED_URI_SUFFIX; URL feedUrl = new URL(commentsFeedUri); Feed resultFeed = myService.getFeed(feedUrl, Feed.class); // Display the results System.out.println(resultFeed.getTitle().getPlainText()); for (int i = 0; i < resultFeed.getEntries().size(); i++) { Entry entry = resultFeed.getEntries().get(i); System.out.println("\t" + ((TextContent) entry.getContent()).getContent().getPlainText()); System.out.println("\t" + entry.getUpdated().toStringRfc822()); } System.out.println(); } /** * Removes the comment specified by the given editLinkHref. * * @param myService An authenticated GoogleService object. * @param editLinkHref The URI given for editing the comment. * @throws ServiceException If the service is unable to handle the request. * @throws IOException If there is an error communicating with the server. */ public static void deleteComment(GoogleService myService, String editLinkHref) throws ServiceException, IOException { URL deleteUrl = new URL(editLinkHref); myService.delete(deleteUrl); } /** * Removes the post specified by the given editLinkHref. * * @param myService An authenticated GoogleService object. * @param editLinkHref The URI given for editing the post. * @throws ServiceException If the service is unable to handle the request. * @throws IOException If there is an error communicating with the server. */ public static void deletePost(GoogleService myService, String editLinkHref) throws ServiceException, IOException { URL deleteUrl = new URL(editLinkHref); myService.delete(deleteUrl); } /** * Runs through all the examples using the given GoogleService instance. * * @param myService An authenticated GoogleService object. * @param userName username of user to authenticate (e.g. jdoe@gmail.com). * @param userPassword password to use for authentication. * @throws ServiceException If the service is unable to handle the request. * @throws IOException If there is an error communicating with the server. */ public static void run(GoogleService myService, String userName, String userPassword) throws ServiceException, IOException { // Authenticate using ClientLogin myService.setUserCredentials(userName, userPassword); // Get the blog ID from the metatfeed. String blogId = getBlogId(myService); feedUri = FEED_URI_BASE + "/" + blogId; // Demonstrate retrieving a list of the user's blogs. printUserBlogs(myService); // Demonstrate how to create a draft post. Entry draftPost = createPost(myService, "Snorkling in Aruba", "<p>We had so much fun snorkling in Aruba<p>", "Post author", userName, true); System.out.println("Successfully created draft post: " + draftPost.getTitle().getPlainText()); // Demonstrate how to publish a public post. Entry publicPost = createPost(myService, "Back from vacation", "<p>I didn't want to leave Aruba, but I ran out of money :(<p>", "Post author", userName, false); System.out.println("Successfully created public post: " + publicPost.getTitle().getPlainText()); // Demonstrate various feed queries. printAllPosts(myService); printDateRangeQueryResults(myService, DateTime.parseDate("2007-04-04"), DateTime.parseDate("2007-04-06")); // Demonstrate two ways of updating posts. draftPost.setTitle(new PlainTextConstruct("Swimming with the fish")); draftPost.update(); System.out.println("Post's new title is \"" + draftPost.getTitle().getPlainText() + "\".\n"); publicPost = updatePostTitle(myService, publicPost, "The party's over"); System.out.println("Post's new title is \"" + publicPost.getTitle().getPlainText() + "\".\n"); // Demonstrate how to add a comment on a post // Get the post ID and build the comments feed URI for the specified post System.out.println("Creating comment"); String selfLinkHref = publicPost.getSelfLink().getHref(); String[] tokens = selfLinkHref.split("/"); String postId = tokens[tokens.length - 1]; Entry comment = createComment(myService, postId, "Did you see any sharks?"); // Demonstrate how to retrieve the comments for a post printAllComments(myService, postId); // Demonstrate how to delete a comment from a post System.out.println("Deleting comment"); deleteComment(myService, comment.getEditLink().getHref()); // Demonstrate two ways of deleting posts. System.out.println("Deleting draft post"); draftPost.delete(); System.out.println("Deleting published post"); deletePost(myService, publicPost.getEditLink().getHref()); } /** * Uses the command line arguments to authenticate the GoogleService and build * the basic feed URI, then invokes all the other methods to demonstrate how * to interface with the Blogger service. * * @param args See the usage method. */ public static void main(String[] args) { // Set username, password and feed URI from command-line arguments. SimpleCommandLineParser parser = new SimpleCommandLineParser(args); String userName = parser.getValue("username", "user", "u"); String userPassword = parser.getValue("password", "pass", "p"); boolean help = parser.containsKey("help", "h"); if (help || (userName == null) || (userPassword == null)) { usage(); System.exit(1); } GoogleService myService = new GoogleService("blogger", "exampleCo-exampleApp-1"); try { run(myService, userName, userPassword); } catch (ServiceException se) { se.printStackTrace(); } catch (IOException ioe) { ioe.printStackTrace(); } } /** * Prints the command line usage of this sample application. */ private static void usage() { System.out.println("Usage: BloggerClient --username <username>" + " --password <password>"); System.out.println("\nA simple application that creates, queries,\n" + "updates and deletes posts and comments on the\n" + "specified blog using the provided username and\n" + "password for authentication."); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -