Created
July 28, 2015 03:31
-
-
Save tangblack/45259645c867b72be438 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The BlogId of a test blog. | |
String TEST_BLOG_ID = "8070105920543249955"; | |
// Configure the Java API Client for Installed Native App | |
HttpTransport HTTP_TRANSPORT = new NetHttpTransport(); | |
JsonFactory JSON_FACTORY = new JacksonFactory(); | |
// set up authorization code flow | |
AuthorizationCodeFlow flow = new AuthorizationCodeFlow.Builder( | |
BearerToken.authorizationHeaderAccessMethod(), | |
HTTP_TRANSPORT, | |
JSON_FACTORY, | |
new GenericUrl("https://accounts.google.com/o/oauth2/token"), | |
new ClientParametersAuthentication("用戶端ID", "用戶端密碼"), | |
"用戶端ID", | |
"https://accounts.google.com/o/oauth2/auth") | |
.setScopes(Arrays.asList(BloggerScopes.BLOGGER)) | |
.setDataStoreFactory(MemoryDataStoreFactory.getDefaultInstance()) | |
.build(); | |
// authorize | |
LocalServerReceiver receiver = new LocalServerReceiver.Builder() | |
.setHost("127.0.0.1") | |
.setPort(8080).build(); | |
Credential credential = new AuthorizationCodeInstalledApp(flow, receiver).authorize("user"); | |
// Construct the Blogger API access facade object. | |
Blogger blogger = Blogger.builder(HTTP_TRANSPORT, JSON_FACTORY) | |
.setApplicationName("Blogger-PostsInsert-Snippet/1.0") | |
.setHttpRequestInitializer(credential).build(); | |
// Construct a post to insert | |
Post content = new Post(); | |
content.setTitle("A test post"); | |
content.setContent("With HTML content"); | |
// The request action. | |
Insert postsInsertAction = blogger.posts().insert(TEST_BLOG_ID, content); | |
// Restrict the result content to just the data we need. | |
postsInsertAction.setFields("author/displayName,content,published,title,url"); | |
// This step sends the request to the server. | |
Post post = postsInsertAction.execute(); | |
// Now we can navigate the response. | |
System.out.println("Title: " + post.getTitle()); | |
System.out.println("Author: " + post.getAuthor().getDisplayName()); | |
System.out.println("Published: " + post.getPublished()); | |
System.out.println("URL: " + post.getUrl()); | |
System.out.println("Content: " + post.getContent()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment