如何获得Google Plus中某篇文章的评论? [英] How to get the comments of a post in Google Plus?

查看:116
本文介绍了如何获得Google Plus中某篇文章的评论?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何获得Google +上的帖子的评论?

我已经获取帖子的内容和回复的数量。

  public class示例{

private static Plus unauthenticatedPlus;

public static void main(String [] args)throws IOException {

try {
setupTransport();

getActivity();
} catch(HttpResponseException e){
/ * ... * /
}
}

/ **
*设置传输我们的API调用。
* @throws java.io.IOException当传输无法创建时
* /
private static void setupTransport()throws IOException {
//这是一个未经验证的Plus目的。如果您
//不需要使用/ me /路径段来发现当前用户的
// ID,则可以使用此代码跳过OAuth流程。
unauthenticatedPlus = Plus.builder(Util.TRANSPORT,Util.JSON_FACTORY)
//当我们不指定访问令牌时,我们必须指定我们的API密钥,而不是
//我们使用JsonHttpRequestInitializer
.setJsonHttpRequestInitializer(new JsonHttpRequestInitializer(){
@Override $ b $ public void initialize(JsonHttpRequest jsonHttpRequest)throws IOException {
PlusRequest plusRequest =(PlusRequest)jsonHttpRequest;
plusRequest .setKey(Auth.GOOGLE_API_KEY);
}
})。build();

/ *授权部分被剥离,因为这个例子中不需要* /
/ * ... * /
}

/ * *
*获取已认证用户的最新活动。
*
* @throws IOException如果无法调用API
* /
private static void getActivity()throws IOException {
//已知的公共活动ID
String activityId =z12gtjhq3qn2xxl2o224exwiqruvtda0i;

/ *通过ID获取明确的公共活动* /
//我们无需通过身份验证即可获取此活动
尝试{
Activity activity = unauthenticatedPlus 。.activities()得到(activityId).execute();
show(activity);
} catch(HttpResponseException e){
/ * ... * /
}
}

/ **
*打印在命令行上指定的活动。
*
* @param activity显示
* /
的活动private static void show(Activity activity){
System.out.println(id: + activity.getId());
System.out.println(url:+ activity.getUrl());
System.out.println(content:+ activity.getPlusObject()。getContent());
System.out.println(No of answers:+ activity.getPlusObject()。getReplies()。getTotalItems());


$ / code $ / pre

预先致谢

解决方案

获取活动评论需要

创建一个新的额外请求。 -java-client.googlecode.com/hg/apis/plus/v1/index.htmlrel =nofollow> PlusRequest 实例:

  Plus.Comments.List listComments = plus.comments()。list(activityId); 

执行请求并将其解析为 CommentFeed

  CommentFeed commentFeed = listComments.execute(); 

获取 评论 实例:

  List< Comment> comments = commentFeed.getItems(); 


How can I get the comments of a post on Google+?

I am already getting the content of the post and the number of replies.

public class Sample {

  private static Plus unauthenticatedPlus;

  public static void main(String[] args) throws IOException {

    try {
      setupTransport();

      getActivity();
    } catch (HttpResponseException e) {
      /* ... */
    }
  }

  /**
   * Setup the transport for our API calls.
   * @throws java.io.IOException when the transport cannot be created
   */
   private static void setupTransport() throws IOException {
       // Here's an example of an unauthenticated Plus object. In cases where you
       // do not need to use the /me/ path segment to discover the current user's
       // ID, you can skip the OAuth flow with this code.
       unauthenticatedPlus = Plus.builder(Util.TRANSPORT, Util.JSON_FACTORY)
           // When we do not specify access tokens, we must specify our API key instead
           // We do this using a JsonHttpRequestInitializer
           .setJsonHttpRequestInitializer(new JsonHttpRequestInitializer() {
               @Override
               public void initialize(JsonHttpRequest jsonHttpRequest) throws IOException {
                 PlusRequest plusRequest = (PlusRequest) jsonHttpRequest;
                 plusRequest.setKey(Auth.GOOGLE_API_KEY);
               }
       }).build();

      /* Authorization part stripped, as this is not needed in this sample */
      /* ... */
  }

  /**
   * Get the most recent activity for the authenticated user.
   *
   * @throws IOException if unable to call API
   */
  private static void getActivity() throws IOException {
    // A known public activity ID
    String activityId = "z12gtjhq3qn2xxl2o224exwiqruvtda0i";

    /* Get an explicit public activity by ID */
    // We do not need to be authenticated to fetch this activity
    try {
      Activity activity = unauthenticatedPlus.activities().get(activityId).execute();
      show(activity);
    } catch (HttpResponseException e) {
      /* ... */
    }
  }

  /**
   * Print the specified activity on the command line.
   *
   * @param activity the activity to show
   */
  private static void show(Activity activity) {
    System.out.println("id: " + activity.getId());
    System.out.println("url: " + activity.getUrl());
    System.out.println("content: " + activity.getPlusObject().getContent());
    System.out.println("No of replies: " + activity.getPlusObject().getReplies().getTotalItems());
  }
}

Thanks in advance.

解决方案

Getting the comments of an activity needs an extra request to the Google+ API.

Create a new PlusRequest instance:

Plus.Comments.List listComments = plus.comments().list(activityId);

Execute the request and parse it into a CommentFeed:

CommentFeed commentFeed = listComments.execute();

Get a list of Comment instances:

List<Comment> comments = commentFeed.getItems();

这篇关于如何获得Google Plus中某篇文章的评论?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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