Click here to see your recently viewed pages and most viewed pages.
Hide
Google+ Platform

Moments: insert

Requires authorization

Record a moment representing a user's action such as making a purchase or commenting on a blog. Writing moments involves specifying the type, which is a moment type, and posting that type of moment's required fields. When authenticating for moments.insert, you must include the data-requestvisibleactions parameter to specify which types of App Activities your application will write. Try it now or see an example.

Request

HTTP request

POST https://www.googleapis.com/plus/v1/people/userId/moments/collection

Parameters

Parameter name Value Description
Path parameters
collection string The collection to which to write moments.

Acceptable values are:
  • "vault": The default collection for writing new moments.
userId string The ID of the user to record actions for. The only valid values are "me" and the ID of the authenticated user.
Optional query parameters
debug boolean Return the moment as written. Should be used only for debugging.

Authorization

This request requires authorization with at least one of the following scopes (read more about authentication and authorization).

Scope
https://www.googleapis.com/auth/plus.login

Request body

In the request body, supply a Moments resource with the following properties:

Property name Value Description Notes
Required Properties
object nested object The object on which the action was performed. Specifying this is equivalent with specifying "target". Note that responses from the server will use the "target" field instead for backward-compatibility with older clients.
type string The schema.org type for the type of moment to write. For example, http://schema.org/AddAction. Note that responses from the server will use the Google schema type instead for backward-compatibility with older clients. For example, http://schemas.google.com/AddActivity. writable
Optional Properties
startDate datetime Time stamp of when the action occurred in RFC3339 format. writable

Response

If successful, this method returns a Moments resource in the response body.

Examples

Note: The code examples available for this method do not represent all supported programming languages (see the client libraries page for a list of supported languages).

Java

Uses the Java client library.

// This sample assumes a client object has been created.
// To learn more about creating a client, check out the starter:
//  https://developers.google.com/+/quickstart/java

// This example shows how to create a moment that is associated with a URL that has schema.org markup.
Moment moment = new Moment();
moment.setType("http://schema.org/AddAction");
ItemScope itemScope = new ItemScope();
itemScope.setUrl("https://developers.google.com/+/plugins/snippet/examples/thing");
moment.setObject(itemScope);
Moment momentResult = service.moments().insert("me", "vault", moment).execute();

// This example shows how to create moment that does not have a URL.
Moment moment = new Moment();
moment.setType("http://schema.org/AddAction");
ItemScope itemScope = new ItemScope();
itemScope.setId("target-id-1");
itemScope.setType("http://schema.org/AddAction");
itemScope.setName("The Google+ Platform");
itemScope.setDescription("A page that describes just how awesome Google+ is!");
itemScope.setImage("https://developers.google.com/+/plugins/snippet/examples/thing.png");
moment.setObject(itemScope);
Moment momentResult = service.moments().insert("me", "vault", moment).execute();

.NET

Uses the .NET client library.

// This sample assumes a client object has been created.
// To learn more about creating a client, check out the starter:
//  https://developers.google.com/+/quickstart/csharp

Moment body = new Moment();
ItemScope itemScope = new ItemScope();

itemScope.Id = "replacewithuniqueforaddtarget";
itemScope.Image = "http://www.google.com/s2/static/images/GoogleyEyes.png";
itemScope.Type = "";
itemScope.Description = "The description for the action";
itemScope.Name = "An example of add activity";

body.Object = itemScope;
body.Type = "http://schema.org/AddAction";

MomentsResource.InsertRequest insert =
    new MomentsResource.InsertRequest(
        plusService,
        body,
        userId,
        MomentsResource.Collection.Vault);
Moment wrote = insert.Fetch();

PHP

Uses the PHP client library.

# This sample assumes a client object has been created.
# To learn more about creating a client, check out the starter:
#  https://developers.google.com/+/quickstart/php

# This example shows how to create a moment that is associated with a URL that has schema.org markup.
$moment_body = new Google_Moment();
$moment_body->setType("http://schema.org/AddAction");
$item_scope = new Google_ItemScope();
$item_scope->setUrl("https://developers.google.com/+/plugins/snippet/examples/thing");
$moment_body->setObject($item_scope);
$momentResult = $plus->moments->insert('me', 'vault', $moment_body);

# This example shows how to create moment that does not have a URL.
$moment_body = new Google_Moment();
$moment_body->setType("http://schema.org/AddAction");
$item_scope = new Google_ItemScope();
$item_scope->setId("target-id-1");
$item_scope->setType("http://schema.org/AddAction");
$item_scope->setName("The Google+ Platform");
$item_scope->setDescription("A page that describes just how awesome Google+ is!");
$item_scope->setImage("https://developers.google.com/+/plugins/snippet/examples/thing.png");
$moment_body->setObject($item_scope);
$momentResult = $plus->moments->insert('me', 'vault', $moment_body);

Python

Uses the Python client library.

# This sample assumes a client object has been created.
# To learn more about creating a client, check out the starter:
#  https://developers.google.com/+/quickstart/python

# This example shows how to create a moment that is associated with a URL that has schema.org markup.
moment = {"type":"http://schema.org/AddAction",
              "object": {
                "url": "https://developers.google.com/+/plugins/snippet/examples/thing"
              }
         }
google_request = service.moments().insert(userId='me', collection='vault', body=moment)
momentResult = google_request.execute()

# This example shows how to create moment that does not have a URL.
moment = {"type":"http://schema.org/AddAction",
          "object": {
            "id": "object-id-1",
            "type":"http://schema.org/AddAction",
            "name": "The Google+ Platform",
            "description": "A page that describes just how awesome Google+ is!",
            "image": "https://developers.google.com/+/plugins/snippet/examples/thing.png"
          }
         }
google_request = service.moments().insert(userId='me', collection='vault', body=moment)
result = google_request.execute()

JavaScript

Uses the JavaScript client library.

// This sample assumes a client object has been created.
// To learn more about creating a client, check out the starter:
//  https://developers.google.com/+/quickstart/javascript
// This example shows how to create a moment that is associated with a URL that
// has schema.org markup.
function writeAddActivity(url){
    var payload = {
      "type":"http:\/\/schema.org\/AddAction",
      "startDate": "2012-10-31T23:59:59.999Z"
    };
    if (url != undefined){
      payload.object = {
        'url' : url
      };
    }else{
    payload.object = {
      "id" : "replacewithuniqueidforaddtarget",
      "image" : "http:\/\/www.google.com\/s2\/static\/images\/GoogleyEyes.png",
      "type" : "http:\/\/schema.org\/CreativeWork",
      "description" : "The description for the action",
      "name":"An example of AddAction"
    };
  }
  gapi.client.plus.moments.insert(
      {  'userId' : 'me',
         'collection' : 'vault',
         'resource' : payload
      }).execute(function(result){
          console.log(result);
      });
}

Try it!

Use the APIs Explorer below to call this method on live data and see the response. Alternatively, try the standalone Explorer.

IMPORTANT! The APIs Explorer below does not currently work. It returns a "401 Unauthorized" error. The method itself works, but this explorer does not pass in the required data-requestvisibleactions. This is a known bug that we plan to fix.