Reading posts and comments

The Google+ Domains API allows your app to list a user's posts, read specific posts, and list comments about on posts. Google+ posts are represented in the Google+ Domains API by activity resources.

To retrieve posts for a user within your domain, your app must include the following scopes:

https://www.googleapis.com/auth/plus.me
Grants the app permission to use the special value me to represent the authenticated user. Does not apply to apps that use domain-wide delegation of authority.
https://www.googleapis.com/auth/plus.stream.read
Required - Grants permission for the app to read Google+ posts and comments by the user, or that are visible to the user. The Google+ Domains API only permits reading posts by people in the same Google Apps domain as the user, and comments on those posts. Comments made by people outside of the Google Apps domain of the user are accessible using the Google+ Domains API if they were made on posts authored by people in the domain.

Getting a list of a user's posts

The following example demonstrates how to list a user's posts. The returned results contain a brief summary of the posts, including a title. Use the Activities.get method to read the full details of a post.

Java

// This sample assumes a client object `plusDomains` has been created
// and that the user you want to retrieve a list of posts for is an
// authenticated user.
// To learn more about creating a client, see the OAuth 2.0 example:
//  https://developers.google.com/+/domains/authentication/

import com.google.api.services.plusDomains.model.Activity;
import com.google.api.services.plusDomains.model.ActivityFeed;

PlusDomains.Activities.List listActivities = plusDomains.activities().list("me", "user");
listActivities.setMaxResults(5L);

// Execute the request for the first page
ActivityFeed activityFeed = listActivities.execute();

// Unwrap the request and extract the pieces we want
List<Activity> activities = activityFeed.getItems();

// Loop through until we arrive at an empty page
while (activities != null) {
  for (Activity activity : activities) {
    System.out.println("ID " + activity.getId() + " Content: " +
            activity.getObject().getContent());
  }

  // We will know we are on the last page when the next page token is null.
  // If this is the case, break.
  if (activityFeed.getNextPageToken() == null) {
    break;
  }

  // Prepare to request the next page of activities
  listActivities.setPageToken(activityFeed.getNextPageToken());

  // Execute and process the next page request
  activityFeed = listActivities.execute();
  activities = activityFeed.getItems();
}

Python

# This sample assumes a client object `service` has been created and
# that the user you want to retrieve a list of posts for is an
# authenticated user.
# To learn more about creating a client, see the OAuth 2.0 example:
#  https://developers.google.com/+/domains/authentication/

user_id = 'me'

activities_service = service.activities()
request = activities_service.list(
    userId=user_id,
    collection='user',
    maxResults='2')

while request is not None:
  activities_document = request.execute()
  if activities_document.get('items') is not None:
    print 'got page with %d' % len(activities_document.get('items'))
    for activity in activities_document.get('items'):
      print activity.get('id'), activity.get('object').get('content')

  request = activities_service.list_next(request, activities_document)

Protocol

Request

GET https://www.googleapis.com/plusDomains/v1/people/{userId}/activities/user

If you user set the {userId} to me, you must use the https://www.googleapis.com/auth/plus.me scope.

Example

The following command demonstrates a request to the Activities.list method and assumes that you already have an OAuth token for your user:

curl -v -H "Content-Type: application/json" -H "Authorization: OAuth$ACCESS_TOKEN" -X GET https://www.googleapis.com/plusDomains/v1/people/me/activities/user

See the Activities.list REST API method.

Reading a specific post

If you need additional information about a post, including the number of +1's, reshares, and comments on the post, you can access the full details by using the Activities.get method.

If you are listing the posts for an authenticated user that granted your app the https://www.googleapis.com/auth/plus.me scope, you can specify the user ID as "me".

Java

// This sample assumes a client object `plusDomains` has been created
// and that you know the ID of the post you want to retrieve.
// To learn more about creating a client, see the OAuth 2.0 example:
//  https://developers.google.com/+/domains/authentication/

import com.google.api.services.plusDomains.model.Activity;

String activityId = "z1234z";

Activity activity = plusDomains.activities().get(activityId).execute();
System.out.println("Used get to fetch activity with ID " + activity.getId());
System.out.println(" Activity content:\t" + activity.getObject().getContent());
System.out.println(" +1's: \t" + activity.getObject().getPlusoners().getTotalItems());
System.out.println(" Reply comments: \t" + activity.getObject().getReplies().getTotalItems());

Python

# This sample assumes a client object `service` has been created and
# that you know the ID of the post you want to retrieve.
# To learn more about creating a client, see the OAuth 2.0 example:
#  https://developers.google.com/+/domains/authentication/

activity_id = 'z1234z'

activities_service = service.activities()
activity = activities_service.get(activityId=activity_id).execute()
print activity.get('id'), activity.get('object').get('content')

Protocol

Request

GET https://www.googleapis.com/plusDomains/v1/activities/{activityId}

Replace {activityId} with the ID of the specific post that you want to get information about. You can get a specific post's ID by using the Activities.list method.

Example

The following command demonstrates a request to the Activities.get method and assumes that you already have an OAuth token for your user:

curl -v -H "Content-Type: application/json" -H "Authorization: OAuth$ACCESS_TOKEN" -X GET https://www.googleapis.com/plusDomains/v1/activities/{activityId}

Getting a list of comments on a specific post

After you get an activity ID, you can list the comments that have been made on that post. The following example gets the comments for a specific post, which is identified by activityId.

Java

// This sample assumes a client object `plusDomains` has been created
// and that you know the ID of the post you want to retrieve the
// comments of.
// To learn more about creating a client, see the OAuth 2.0 example:
//  https://developers.google.com/+/domains/authentication/

import com.google.api.services.plusDomains.model.Comment;
import com.google.api.services.plusDomains.model.CommentFeed;

String activityId = "z1234z";
PlusDomains.Comments.List listComments = plusDomains.comments().list(activityId);
listComments.setMaxResults(5L);

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

// Loop through until we arrive at an empty page
while (comments != null) {
  for (Comment comment : comments) {
    System.out.println(comment.getActor().getDisplayName() + " commented " +
            comment.getObject().getContent());
  }

  // We will know we are on the last page when the next page token is null.
  // If this is the case, break.
  if (commentFeed.getNextPageToken() == null) {
    break;
  }

  // Prepare the next page of results
  listComments.setPageToken(commentFeed.getNextPageToken());

  // Execute and process the next page request
  commentFeed = listComments.execute();
  comments = commentFeed.getItems();
}

Python

# This sample assumes a client object `service` has been created and
# that you know the ID of the post you want to retrieve the comments
# of.
# To learn more about creating a client, see the OAuth 2.0 example:
#  https://developers.google.com/+/domains/authentication/

activity_id = 'z1234'

comments_service = service.comments()
request = comments_service.list(
    maxResults=10,
    activityId=activity_id)

while request is not None:
  comments_list = request.execute()
  if comments_list.get('items') is not None:
    print 'got page with %d' % len(comments_list.get('items'))
    for comment in comments_list.get('items'):
      print comment.get('id'), comment.get('object').get('content')

  request = comments_service.list_next(request, comments_list)

Protocol

Request

GET https://www.googleapis.com/plusDomains/v1/activities/{activityId}/comments

Replace {activityId} with the ID of the specific post for which you want to retrieve the comments. For your convenience, this URL is returned in the response of the activities.list method in the replies.selfLink property.

Example

The following command demonstrates a request to the Comments.list method and assumes that you already have an OAuth token for your user:

curl -v -H "Content-Type: application/json" -H "Authorization: OAuth$ACCESS_TOKEN" -X GET https://www.googleapis.com/plusDomains/v1/activities/{activityId}/comments

See the Comments.list REST API method.

Enviar comentarios sobre…