Blogger API示例代码 [英] Blogger API Sample Code

查看:103
本文介绍了Blogger API示例代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这是我关于StackOverflow的第一个问题,所以请让我知道是否有我忽略的东西!

So this is my first question on StackOverflow, so please let me know if there's something I've neglected to include!

我正试图从公共Blogger博客中获取Blog帖子数据,以进行一些语言分析研究.尽管Java API看起来很简单,但我发现Google的代码示例位于 https://developers.google.com/blogger/docs/3.0/reference/posts/list#examples 不起作用,因为缺少许多依赖项,从LocalServerReceiver()到OAuthorization所需的全部依赖项. API资源管理器工作正常,但是很明显,我需要一些自己的代码.

I'm trying to get Blog post data from public Blogger blogs for some language analysis research that I'm doing. Although the java API seems pretty straight-forward, I've found that Google's code sample at https://developers.google.com/blogger/docs/3.0/reference/posts/list#examples does not work, as there are many dependencies missing, ranging from LocalServerReceiver() to a whole host of dependencies needed for OAuthorization. The API explorer works just fine, but obviously, I need something for my own code.

我还尝试利用其他StackOverflow问题中的代码片段,这些问题与我的发现相似,并且仍然面临依赖问题.

I also tried utilizing code fragments from other StackOverflow questions that I saw were similar to mine, and am still facing dependency issues.

这是我所研究的一些问题的列表,由于某种代码弃用,这些问题未能解决我的问题:

Here's a list of some of the questions I've looked at, which have not solved my issue due to some sort of code deprecation:

为什么Java不这样做允许我在这里使用OAuth2Native方法吗?

我已经使用OAuthPlayground来获取授权代码,并一直尝试在

I've used the OAuthPlayground to get an authorization code, and have been trying to replicate some of the functionality of iamkhova's solution in Proper Form of API request to Blogger using Java/App Engine -error 401. Note that I'm not actually trying to write anything to any of the blogs I'm accessing. I just want to be able to get the post data for analysis.

目前,我刚刚通过删除记录器并添加了一个getPosts()函数来更改iamkhova的解决方案,该函数可复制我从Google的示例代码中获取的内容.

Currently I've just altered iamkhova's solution by taking out the logger, and adding a getPosts() function that duplicates what I need from Google's sample code.

   public class BlogHandler
{
  static final String API_KEY = {My API Key};
  public Blogger blogger = null;
  public Blog blog;
  public java.util.List<Post> posts;

  public BlogHandler() {}

  public void executeGetBlogByUrl (String url) throws IOException {
     GetByUrl request = blogger.blogs().getByUrl( url );
     this.blog = request.setKey(API_KEY).execute();

   }
  public void getPosts() throws IOException
  {
      List postsListAction = blogger.posts().list(this.blog.getId());

    // Restrict the result content to just the data we need.
    postsListAction.setFields("items(author/displayName,content,published,title,url),nextPageToken");

    // This step sends the request to the server.
    PostList posts = postsListAction.execute();

    // Now we can navigate the response.
    int postCount = 0;
    int pageCount = 0;
    while (posts.getItems() != null && !posts.getItems().isEmpty()) {
            for (Post post : posts.getItems()) {
                    System.out.println("Post #"+ ++postCount);
                    System.out.println("\tTitle: "+post.getTitle());
                    System.out.println("\tAuthor: "+post.getAuthor().getDisplayName());
                    System.out.println("\tPublished: "+post.getPublished());
                    System.out.println("\tURL: "+post.getUrl());
                    System.out.println("\tContent: "+post.getContent());
            }

            // Pagination logic
            String pageToken = posts.getNextPageToken();
            if (pageToken == null || ++pageCount >= 5) {
                    break;
            }
            System.out.println("-- Next page of posts");
            postsListAction.setPageToken(pageToken);
            posts = postsListAction.execute();
    }

  }

   public void setupService () throws IOException {

    AppIdentityCredential credential = null;
    credential  = new AppIdentityCredential(Arrays.asList(BloggerScopes.BLOGGER)); // Add your scopes here
    this.blogger = new Blogger.Builder(new UrlFetchTransport(), new JacksonFactory(), credential).setApplicationName("chsBlogResearch").build();
   }

}

当前,我遇到以下错误:

Currently, I'm having the following error:

Exception in thread "main" com.google.apphosting.api.ApiProxy$CallNotFoundException: The API package 'memcache' or call 'Get()' was not found.
    at com.google.apphosting.api.ApiProxy$1.get(ApiProxy.java:173)
    at com.google.apphosting.api.ApiProxy$1.get(ApiProxy.java:171)
    at com.google.appengine.api.utils.FutureWrapper.get(FutureWrapper.java:89)
    at com.google.appengine.api.memcache.MemcacheServiceImpl.quietGet(MemcacheServiceImpl.java:26)
    at com.google.appengine.api.memcache.MemcacheServiceImpl.get(MemcacheServiceImpl.java:49)
    at com.google.appengine.api.appidentity.AppIdentityServiceImpl.getAccessToken(AppIdentityServiceImpl.java:286)
    at com.google.api.client.googleapis.extensions.appengine.auth.oauth2.AppIdentityCredential.intercept(AppIdentityCredential.java:98)
    at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:859)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
    at BloggerData.BlogHandler.executeGetBlogByUrl(BlogHandler.java:29)

单击代码行以查看MemcacheServiceImpl和AppIdentityServiceImpl中的两个错误,告诉我那时没有代码行.我正在Eclipse中使用Maven进行依赖.

Clicking on the code lines for both the errors in MemcacheServiceImpl and AppIdentityServiceImpl tell me that there are no lines of code at that point. I'm using Maven within Eclipse for dependencies.

我在代码中真正不确定的唯一一件事就是范围的概念,但是我不认为那会导致我的错误.

The only thing I'm not really sure about in this code is the idea of scopes, but I don't think that that should be causing my errors.

对此我将不胜感激,因为获取帖子数据比我想象的要耗时得多!

I would appreciate any ideas on this, as getting this post data has been way more time-consuming than I thought it would be!

更新:获取奇怪的异常尝试在Google App Engine for Java中实现异步http 可以对上述错误提供更多的见解.显然,无法通过控制台应用程序调用此ApiProxy jar.

Update: getting strange exception trying to implement asynchronous http in google app engine for java provided a little more insight on the error above. Apparently this ApiProxy jar cannot be called through a console app.

推荐答案

这并不是一个非常有用的答案,但这正是我所遇到的问题.

Not really a super helpful answer, but it's what ended up working in my situation.

Google Java API客户端已过时,因此我最终改用Google API Python客户端,因为它更新得更好,并且OAuth实际上可以在Python客户端中使用.它位于: https://github.com/google/google-google-api-python-client .样本文件非常有用,而且非常直观.

The Google Java API client is extremely out of date, so I ended up switching to the Google API Python client instead, since it's updated better, and OAuth actually works in the Python client. It's located at: https://github.com/google/google-api-python-client. The sample files are very useful, and actually intuitive.

请注意,至少在Blogger方面,Google的Java API示例均已损坏.

Note that Google's Java API samples are all broken, at least on the Blogger side of things.

这篇关于Blogger API示例代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆