Creating new posts and comments

The Google+ Domains API allows your app to create new posts and add comments to existing posts.

To create 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.write
Required - Grants permission for the app to create posts or comments on behalf of a user. The Google+ Domains API only allows creation of restricted posts, and only allows comments to be added to restricted posts.

Creating a post

The following example demonstrates how to create a post that is available to all users within your Google Apps domain.

Java

// This sample assumes a client object `plusDomains` has been created
// and your app has an access token to use on behalf of the 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.Acl;
import com.google.api.services.plusDomains.model.Activity;
import com.google.api.services.plusDomains.model.PlusDomainsAclentryResource;

String msg = "Happy Monday! #caseofthemondays";

// Create a list of ACL entries
PlusDomainsAclentryResource resource = new PlusDomainsAclentryResource();
resource.setType("domain"); // Share to domain

List<PlusDomainsAclentryResource> aclEntries =
    new ArrayList<PlusDomainsAclentryResource>();
aclEntries.add(resource);

Acl acl = new Acl();
acl.setItems(aclEntries);
acl.setDomainRestricted(true);  // Required, this does the domain restriction

// Create a new activity object
Activity activity = new Activity()
    .setObject(new Activity.PlusDomainsObject().setOriginalContent(msg))
    .setAccess(acl);

// Execute the API request, which calls `activities.insert` for the logged in user
activity = plusDomains.activities().insert("me", activity).execute();

Python

# This sample assumes a client object `service` has been created
# and your app has an access token to use on behalf of the user.
# To learn more about creating a client, see the OAuth 2.0 example:
#  https://developers.google.com/+/domains/authentication/

import pprint

# Set the user's ID to 'me': requires the plus.me scope
user_id = 'me'

# Insert an Activity
print('Insert activity')
result = service.activities().insert(
    userId = user_id,
    body = {
        'object' : {
            'originalContent' : 'Happy Monday! #caseofthemondays'
        }
        'access' : {
            'items' : [{
                'type' : 'domain'
            }],
            'domainRestricted': True
        }
    }).execute()
print('result = %s' % pprint.pformat(result))

Protocol

Request

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

The following example is the JSON request body for posting an activity (Activities.insert):

{
  "object": {
    "originalContent": "Happy Monday! #caseofthemondays",
  },
  "access": {
    "items": [{
        "type": "domain"
    }],
    "domainRestricted": true
  }
}

Example

The following command demonstrates a request to the Activities.insert 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" -d "{"object": {"originalContent": "Happy Monday!#caseofthemondays"},"access":{"kind":"plus#acl","items":[{"type":"domain"}],"domainRestricted":true}}" -X POST https://www.googleapis.com/plusDomains/v1/people/{userId}/activities

See the Activities.insert REST API method.

For all posts that are created through the API, the posts are restricted to users within the domain. You can choose how broadly within the domain that you want the post to be available. For more information, see access control.

Sharing a post with a specific circle

You can create posts and restrict their visibility to a specific circle. The following code demonstrates looking up a circle named "Tech support" and sharing a post with the users in that circle who are also in the domain. Note that looking up a circle also requires the https://www.googleapis.com/auth/plus.circles.read scope.

Java

// This sample assumes a client object `plusDomains` has been created
// and your app has an access token to use on behalf of the 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.Acl;
import com.google.api.services.plusDomains.model.Activity;
import com.google.api.services.plusDomains.model.Circle;
import com.google.api.services.plusDomains.model.CircleFeed;
import com.google.api.services.plusDomains.model.PlusDomainsAclentryResource;

PlusDomains.Circles.List listCircles = plusDomains.circles().list("me");
listCircles.setMaxResults(5L);
CircleFeed circleFeed = listCircles.execute();
List<Circle> circles = circleFeed.getItems();

String circleId = "";

// Loop through until we arrive at an empty page
while (circles != null) {
  for (Circle circle : circles) {
    if (circle.getDisplayName().equals("Tech support")) {
      circleId = circle.getId();
      break;
    }
  }

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

  // Prepare the next page of results
  listCircles.setPageToken(circleFeed.getNextPageToken());

  // Execute and process the next page request
  circleFeed = listCircles.execute();
  circles = circleFeed.getItems();
}

String msg = "I need help with creating my TPS reports!";

// Create a list of ACL entries
PlusDomainsAclentryResource circleEntry = new PlusDomainsAclentryResource();
circleEntry.setType("circle"); // Share to a specific circle
circleEntry.setId(circleId);

List<PlusDomainsAclentryResource> aclEntries =
    new ArrayList<PlusDomainsAclentryResource>();
aclEntries.add(circleEntry);

Acl acl = new Acl();
acl.setItems(aclEntries);
acl.setDomainRestricted(true); // Required, this does the domain restriction

Activity activity = new Activity()
    .setObject(new Activity.PlusDomainsObject().setOriginalContent(msg))
    .setAccess(acl);

// Execute the API request, which calls `activity.insert` for the logged in user
activity = plusDomains.activities().insert("me", activity).execute();

Python

# This sample assumes a client object `service` has been created and
# your app has an access token to use on behalf of the user.
# To learn more about creating a client, see the OAuth 2.0 example:
#  https://developers.google.com/+/domains/authentication/

import pprint

# Set the user's ID to 'me': requires the plus.me scope
user_id = 'me'

# This example does a circle.list to choose a circle first
circle_id = None
circle_service = service.circles()
request = circle_service.list(userId=user_id)

while request is not None:
  circle_list = request.execute()

  if circle.get('items') is not None:
    print 'Google+ circles for the current user: '
    circles = circle_list.get('items')
    for circle in circles:
      if circle.get('displayName') == 'Tech support':
        circle_id = circle.get('id')
        break

  request = circle_service.list_next(request, circle_list)

message = 'I need help with creating my TPS reports!'

# Insert an Activity
print('Insert activity')
result = service.activities().insert(
    userId = user_id,
    body = {
        'object' : {
            'originalContent' : message
        },
        'access' : {
            'items' : [
                { 'type' : 'circle', 'id' : circle_id}
            ],
            'domainRestricted' : True
        }
    }).execute()
print('result = %s' % pprint.pformat(result))

Protocol

Request

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

The following example is the JSON request body for posting an activity (Activities.insert):

{
  "object": {
    "originalContent": "We\'re putting new coversheets on all the TPS reports before they go out now."
  },
  "access": {
    "items": [
        { "type": "circle", "id": "1234" }
    ],
    "domainRestricted": true
  }
}

You can list multiple audiences within the access.items list.

Example

The following command demonstrates a request to the Activities.insert 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" -d "{"object": {"originalContent": "We're putting new coversheets on all the TPS reports before they go out now.",},"access":{"items": [{ "type": "circle", "id": "1234" }],"domainRestricted": true}}" -X POST https://www.googleapis.com/plusDomains/v1/people/{userId}/activities

See the Activities.insert REST API method.

Sharing a post with a specific person

In addition to sharing with a specific circle, you can also share posts with a specific person, which will also send the person an email notification by default. The following code demonstrates sharing a post with a specific person and assumes that you have already looked up the person's Google ID. This example also demonstrates how to mention the user in the content of the post.

Java

// This sample assumes a client object `plusDomains` has been created
// and your app has an access token to use on behalf of the 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.Acl;
import com.google.api.services.plusDomains.model.Activity;
import com.google.api.services.plusDomains.model.PlusDomainsAclentryResource;

// Assumes you already know the ID that you want to share this post.
String shareToId = "1234";

// If you want to mention someone within the text of a post, you can
// add "+NNNNNN" as shown here:
String msg = "Hey +" + shareToID + " I hear that you're the expert on TPS reports."
    + "Can you help me out?";

// Create a list of ACL entries
PlusDomainsAclentryResource personEntry = new PlusDomainsAclentryResource();
personEntry.setType("person"); // Share to a person
personEntry.setId(shareToId);

List<PlusDomainsAclentryResource> aclEntries =
    new ArrayList<PlusDomainsAclentryResource>();
aclEntries.add(personEntry);

Acl acl = new Acl();
acl.setItems(aclEntries);
acl.setDomainRestricted(true); // Required, this does the domain restriction

Activity activity = new Activity()
    .setObject(new Activity.PlusDomainsObject().setOriginalContent(msg))
    .setAccess(acl);

// Execute the API request, which calls `activity.insert` for the logged in user
activity = plusDomains.activities().insert("me", activity).execute();

Python

# This sample assumes a client object `service` has been created and
# your app has an access token to use on behalf of the user.
# To learn more about creating a client, see the OAuth 2.0 example:
#  https://developers.google.com/+/domains/authentication/

import pprint

# Set the user's ID to 'me': requires the plus.me scope
user_id = 'me'
share_to_id = '1234'

message = ("Hey +%s, I hear that you're the expert on TPS reports. Can"
           " you help me out?" % share_to_id)

# Insert an Activity
print('Insert activity')
result = service.activities().insert(
    userId = user_id,
    body = {
        'object' : {
            'originalContent' : message
        },
        'access' : {
            'items' : [
                { 'type' : 'person', 'id' : share_to_id}
            ],
            'domainRestricted' : True
        }
    }).execute()
print('result = %s' % pprint.pformat(result))

Protocol

Request

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

The following example is the JSON request body for posting an activity and sharing the activity with a specific user who has the user ID of 4567:

{
  "object": {
    "content": "Hey +{shareToId} I hear that you're the expert on TPS reports."
        + " Can you help me out?"
  },
  "access": {
    "items": [
        { "type": "person", "id": "4567" }
    ],
    "domainRestricted": true
  }
}

You can list multiple audiences within the access.items list.

Example

The following command demonstrates a request to the Activities.insert 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" -d "{"object": {"content": "Hey +{shareToId} I hear that you're the expert on TPS reports. Can you help me out?"},"access": {"items": [{"type": "person", "id": "4567" }],"domainRestricted": true}}" -X POST https://www.googleapis.com/plusDomains/v1/people/{userId}/activities

See the Activities.insert REST API method.

Adding comments

The Google+ Domains API enables apps to comment on posts that are restricted within your Google Apps domain.

The following code demonstrates posting a comment on a specific post that was made within the domain and is identified by activityId. See reading posts to learn how to get and list posts by a user.

Java

// This sample assumes a client object `plusDomains` has been created
// and that you know the ID of the post you wish to fetch 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;

String activityId = "z1234z";

String commentMsg = "Yes, I can help with your TPS reports.";

Comment comment = new Comment();
comment.setObject(new Comment.PlusDomainsObject().setOriginalContent(commentMsg));

// Execute the API request, which calls `comment.insert`
comment = plusDomains.comments().insert(activityId, comment).execute();

Python

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

activity_id = 'z1234z'

# Insert an comment on the activity identified by `activityId`
print('Insert comment')
result = service.comments().insert(
    activityId = activity_id,
        body = {
            'object' : {
                'originalContent' : 'Yes, I can help with your TPS reports.'
            }
        }).execute()
print('result = %s' % pprint.pformat(result))

Protocol

Request

POST https://www.googleapis.com/plusDomains/v1/activity/{activityId}/comments

The following example is the JSON request body for posting a comment on a specific activity that has an activity ID of z1234z:

{
  "object": {
    "originalContent": "Yes, I can help with your TPS reports."
  }
}

You can mention users in the comments by specifying their user ID in the originalContent text in the following format: +userId.

Example

The following command demonstrates a request to the Comments.insert 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" -d "{"object": {"originalContent": "Yes, I can help with your TPS reports"}}" -X POST https://www.googleapis.com/plusDomains/v1/activities/z1234z/comments

See the Comments.insert REST API method.

Enviar comentarios sobre…