Loading...

[BLOGGER API]全部のpostが欲しい

javaです。
blogger apiです。
あるブログから全部のポストを取得したいです。
一回でとれるapiはないようなので、一回取得したら、nextPageTokenを使って取得していきます。

とりあえず、自分が使いやすかなーと思う感じのユーティリティーを作ってやってます。


package myblogger; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.net.URISyntaxException; import java.security.GeneralSecurityException; import java.util.ArrayList; import java.util.List; import com.google.api.client.auth.oauth2.Credential; import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp; import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver; import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow; import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets; import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.http.HttpTransport; import com.google.api.client.json.JsonFactory; import com.google.api.client.json.jackson2.JacksonFactory; import com.google.api.client.util.store.FileDataStoreFactory; import com.google.api.services.blogger.Blogger; import com.google.api.services.blogger.BloggerScopes; import com.google.api.services.blogger.Blogger.Blogs; import com.google.api.services.blogger.model.Blog; import com.google.api.services.blogger.model.Post; import com.google.api.services.blogger.model.PostList; /** * * @author s.nakawaka * */ public class BloggerUtils { public static final JsonFactory JSON_FACTORY = JacksonFactory .getDefaultInstance(); /** * * @param applicatioName * @param authorize * @param clinetSecretsJson * @param dataStoreDir * @return * @throws GeneralSecurityException * @throws IOException * @throws URISyntaxException */ public static Blogger blogger(String applicatioName, String authorize, File clinetSecretsJson, File dataStoreDir) throws GeneralSecurityException, IOException, URISyntaxException { FileDataStoreFactory dataStoreFactory = new FileDataStoreFactory( dataStoreDir); // Set up the HTTP transport and JSON factory HttpTransport httpTransport = GoogleNetHttpTransport .newTrustedTransport(); // Load client secrets GoogleClientSecrets clientSecrets = GoogleClientSecrets.load( JSON_FACTORY, new FileReader(clinetSecretsJson)); // Set up authorization code flow // OAuth 2.0 scopes. GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder( httpTransport, JSON_FACTORY, clientSecrets, BloggerScopes.all()) .setDataStoreFactory(dataStoreFactory).build(); // Authorize Credential credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize(authorize); Blogger.Builder builder = new Blogger.Builder(httpTransport, JSON_FACTORY, credential).setApplicationName(applicatioName); Blogger blogger = builder.build(); return blogger; } public static Blog blog(Blogger blogger, String blogId) throws IOException { Blogs blogs = blogger.blogs(); return blogs.get(blogId).execute(); } /** * 次から次へとPostを読み込みます。 * @param list * @return * @throws IOException */ public static List<Post> getAllPost( com.google.api.services.blogger.Blogger.Posts.List list) throws IOException { List<Post> listOfPost = new ArrayList<Post>(); String nextPageToken = null; while (true) { PostList postList = list.execute(); listOfPost.addAll(postList.getItems()); if ((nextPageToken = postList.getNextPageToken()) == null) break; list.setPageToken(nextPageToken); } return listOfPost; } }
リアクション: 
java 8639465541557924114

コメントを投稿

ホーム item

このブログを検索

Random Posts

Popular Posts

Labels

ADS